Have you considered using the POSTGRES_DB environment variable ?
services:
db:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
PGDATA: /data/postgres
POSTGRES_DB: bank
volumes:
- db:/data/postgres
ports:
- "5332:5432"
networks:
- db
restart: unless-stopped
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d postgres" ]
interval: 30s
timeout: 10s
retries: 5
networks:
db:
driver: bridge
volumes:
db:
Answer from e741af0d41bc74bf854041f1fbdbf on Stack OverflowDocker Docs
docs.docker.com › reference › samples › postgresql samples
PostgreSQL samples | Docker Docs
December 2, 2024 - These samples offer a starting point for how to integrate different services using a Compose file.
GitHub
github.com › gaelgthomas › docker-compose-with-postgresql
GitHub - gaelgthomas/docker-compose-with-postgresql: A Docker-Compose file with PostgreSQL ready to use. · GitHub
Starred by 28 users
Forked by 18 users
Languages Shell
Postgres with Docker and Docker compose a step-by-step guide for beginners
Hey thanks a lot per this tutorial u/geshan ... it is exactly what I am working on in a project. IF you allow me I would like to ask you a question .. How we could access Postgres database running in a container from a Node.js application also inside another container but running in another HOST in the same Network the one where Postgres is running ? My use case is like that : MacOS 1 and MacOS 2 on the same LAN MacOS 1 : Run Docker Desktop and I have the Postgres Container MacOS 2 : Run Docker Desktop and I have my NodeJs app Container I would like to use docker-compose Best Regards More on reddit.com
Best practice for Postgres setup with docker-compose - DevOps Stack Exchange
Does anyone have a better way of setting up a postgres server with a known configuration inside a docker-compose based system? ... First, use alpine images if possible. Second, use ENV variables for prepare database configuration. More info. Third, use /docker-entrypoint-initdb.d directory for extend image. More info. Example ... More on devops.stackexchange.com
Share Example Docker Compose for Personal Projects with Go and Postgres?
take a look at one from my article here: https://github.com/sadensmol/article_my_clean_architecture_go_application/blob/master/infrastructure/local/docker-compose.yaml More on reddit.com
Docker Makes Setting Up PostgreSQL Super Easy!
Your page doesn't load for me but I just use a basic docker compose file like below for my container. This uses best practices, enables persistent volume, uses SSL, logging & enables pg_stat_statements. --- services: postgres: image: postgres:17.4 container_name: postgres hostname: postgres environment: POSTGRES_USER_FILE: /run/secrets/pg_user POSTGRES_DB: postgres POSTGRES_PASSWORD_FILE: /run/secrets/pg_pw TZ: America/Chicago PGTZ: America/Chicago secrets: - pg_user - pg_pw volumes: - ./data:/var/lib/postgresql/data - ./logs:/var/log/postgresql - ./certs:/var/lib/postgresql/certs command: > postgres -c ssl=on -c ssl_cert_file=/var/lib/postgresql/certs/server.crt -c ssl_key_file=/var/lib/postgresql/certs/server.key -c logging_collector=on -c log_directory=/var/log/postgresql -c log_filename=postgresql.log -c log_statement=all -c log_connections=on -c log_disconnections=on -c log_destination=stderr,csvlog -c log_rotation_age=1d -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all restart: unless-stopped network_mode: "host" secrets: pg_user: file: ./secrets/pg_user.txt pg_pw: file: ./secrets/pg_pw.txt ... More on reddit.com
10:08
Run PostgreSQL in Docker with Just One YAML File (Super Fast Setup!)
14:05
Setting up PostgreSQL with Docker | Express API & Prisma ORM Query ...
13:50
Docker: How to Initializing PostgreSQL with Seed Files Using Docker ...
Install Postgres Using Docker Compose | Docker | Postgres - YouTube
14:06
Connect a Node Server to Postgres with Docker Compose - YouTube
06:17
How to create a docker-compose setup with PostgreSQL and pgAdmin4 ...
Top answer 1 of 4
28
Have you considered using the POSTGRES_DB environment variable ?
services:
db:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
PGDATA: /data/postgres
POSTGRES_DB: bank
volumes:
- db:/data/postgres
ports:
- "5332:5432"
networks:
- db
restart: unless-stopped
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d postgres" ]
interval: 30s
timeout: 10s
retries: 5
networks:
db:
driver: bridge
volumes:
db:
2 of 4
11
You can use a create db script and add it as a volume, like this:
version: '3'
services:
db:
image: postgres:15.3-alpine3.18
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- ./db:/var/lib/postgresql/data
- ./create-db.sql:/docker-entrypoint-initdb.d/create_database.sql
networks:
- backnet
My SQL script is simple as this:
CREATE DATABASE some_database;
If you already have your volume initiated 'db', you'll need to erase it first (afaik) because postgres will declare that a database already exists and will skip initialization.
Docker
docker.com › blog › how to use the postgres docker official image | docker
How to Use the Postgres Docker Official Image | Docker
November 6, 2024 - Borrowing from our docs on controlling startup and shutdown order, your expanded Compose file might look like this: services: web: build: . ports: - "80:8000" depends_on: db: condition: service_healthy command: ["python", "app.py"] db: image: postgres restart: always environment: POSTGRES_PASSWORD: example healthcheck: test: ["CMD-SHELL", "pg_isready"] interval: 1s timeout: 5s retries: 10 adminer: image: adminer restart: always ports: - 8080:8080
Reddit
reddit.com › r/docker › postgres with docker and docker compose a step-by-step guide for beginners
r/docker on Reddit: Postgres with Docker and Docker compose a step-by-step guide for beginners
December 25, 2021 -
https://geshan.com.np/blog/2021/12/docker-postgres/
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
# Use postgres/example user/password credentials services: db: image: postgres restart: always # set shared memory limit when using docker compose shm_size: 128mb # or set shared memory limit when deploy via swarm stack #volumes: # - type: tmpfs # target: /dev/shm # tmpfs: # size: 134217728 # 128*2^20 bytes = 128Mb environment: POSTGRES_PASSWORD: example adminer: image: adminer restart: always ports: - 8080:8080 Copy
DEV Community
dev.to › xandecodes › spinning-up-postgresql-with-docker-compose-dashboard-g1m
Spinning Up PostgreSQL with Docker Compose + Dashboard - DEV Community
April 21, 2025 - services: database_postgres: image: postgres:latest ports: - 5432:5432 environment: POSTGRES_PASSWORD: password POSTGRES_USER: username POSTGRES_DB: database_name volumes: - ${HOME}/postgres-data/:/var/lib/postgresql/data pgweb: container_name: pgweb restart: always image: sosedoff/pgweb ports: - 8081:8081 links: - database_postgres:database_postgres environment: - PGWEB_DATABASE_URL=postgres://username:password@database_postgres:5432/database_name?sslmode=disable depends_on: - database_postgres · Nothing new under the sun here. Just some variables to initialize the database with a user. But the trick is in the volumes: Here we make sure the data is saved in a directory inside your HOME, so all database data will persist there. 👉 This way, if you run docker-compose down (or docker compose down, depending on your version), you won’t lose your data.
GitHub
github.com › asaikali › docker-compose-postgres
GitHub - asaikali/docker-compose-postgres: Developer friendly docker-compose Postgres Setup · GitHub
Docker Compose v2.23.1+ supports inline config files using the top-level configs section with a content field. This lets you define small configuration files directly inside compose.yaml instead of maintaining separate files on disk. Compose mounts the content as a read-only file inside the container at the path specified by target. configs: postgres_init: content: | CREATE DATABASE demo2; \c demo2 CREATE EXTENSION vector; pgadmin_servers: content: | { "Servers": { "1": { "Name": "Docker Compose", "Group": "Servers", "Port": 5432, "Username": "postgres", "Host": "postgres", "SSLMode": "prefer", "MaintenanceDB": "postgres", "PassFile": "/tmp/pgpassfile" } } }
Starred by 79 users
Forked by 29 users
Languages Shell 70.8% | Java 29.2%
GitHub
gist.github.com › onjin › 2dd3cc52ef79069de1faa2dfd456c945
example docker compose for postgresql with db init script · GitHub
I had the same issue but after a few hours of trying, I found a working combination of docker-compose and init script. Btw, I read here that the container shutting down and restarting is (apparently) part of the initialization process. This is part of my initdb.sh: #!/bin/bash psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL create schema if not exists $SCHEMA; create table $SCHEMA.todos ( id serial primary key, done boolean not null default false, task text not null, due timestamptz ); create role $ANON nologin; create role $AUTHENTICATOR noinherit login password '$POSTGRES_PASSWORD'; grant $ANON to $AUTHENTICATOR; EOSQL
CommandPrompt Inc.
commandprompt.com › education › how-to-create-a-postgresql-database-in-docker
How to Create a PostgreSQL Database in Docker — CommandPrompt Inc.
June 12, 2024 - To create a PostgreSQL database in Docker, use the “CREATE DATABASE <database-name>;” command. Then, create a new table in it and insert values in the table.
Call +1-503-667-4564
Address 2950 Newmarket ST STE 101 - 231, 98226, Bellingham
Elixir Forum
elixirforum.com › t › connecting-to-postgres-in-docker-compose › 65974
Connecting to Postgres in docker-compose | Elixir Forum
September 10, 2024 - GitHub – it uses docker-compose to spin up both a version of Postgres (one specifically with postgis installed) and an instance of RabbitMQ.
DEV Community
dev.to › skipperhoa › install-postgresql-using-docker-compose-44jn
Install PostgreSQL Using Docker Compose - DEV Community
October 17, 2023 - version: '3.8' services: postgres_db: image: postgres:13.5 container_name: PostgresCount restart: always environment: - POSTGRES_USER=hoadev - POSTGRES_PASSWORD=hoadev123 - POSTGRES_DB=hoadev_db volumes: - postgres_db:/var/lib/postgresql/data ports: - '5432:5432' volumes: postgres_db: driver: local · version : specifies the version of the Docker Compose file.