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
10:08
Run PostgreSQL in Docker with Just One YAML File (Super Fast Setup!)
Install Postgres Using Docker Compose | Docker | Postgres - YouTube
14:06
Connect a Node Server to Postgres with Docker Compose - YouTube
05:16
Setting up Docker Compose with Postgres - YouTube
06:17
How to create a docker-compose setup with PostgreSQL and pgAdmin4 ...
16:00
Build a Complete .NET 10 Dev Environment with Docker Compose - YouTube
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 - Dumb question and one I used to know the answer to… but how does one connect to a postgres instance running inside docker-compose? I thought I would work through Broadway’s example repo GitHub - elixir-broadway/broadway_bike_sharing_rabbitmq_example: An example of a Broadway pipeline for a bike sharing app with RabbitMQ and PostgreSQL ·
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.