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
Docker samples for PostgreSQL.
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.
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
How to connect to PostgreSQL using docker-compose?
Looks like you need to initialize Postgres instance since you’re using an external data volume
More on reddit.comTricks for PostgreSQL and Docker that will make your life easier
Another thing I've found to work nicely for testing/debugging is to spin up a pgadmin container on the same docker network More on reddit.com
Build and restore a postgres db from git in a docker compose container
How do you distribute the Dockerfile and such to begin with? Do you have that in git as well? You have a few options. In the Dockerfile, COPY the SQL file into the container, then load it up with pgsql. You can delete it afterward to free up space if it's large. Same as above, but instead of COPY, just use a bind mount or volume to get access to the dump file, so that you don't need to waste space for it in the container. Run a script from the Dockerfile that uses pg_dump | pg_restore/psql to bring down a new copy of the db from a running server elsewhere, like staging. Setup pg once somewhere, then shut it down and copy the data directory. Bring it down to the container with rsync from the Dockerfile. There are probably other ways too if you think about it for a bit. More on reddit.com
Videos
Install Postgres Using Docker Compose | Docker | Postgres - YouTube
10:08
Run PostgreSQL in Docker with Just One YAML File (Super Fast Setup!)
12:23
Run Postgres in a Docker Container (Easiest PostgreSQL Setup) - ...
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 ...
19:23
Django PostgreSQL | Creating a new PostgreSQL Docker Container ...
Docker
docker.com › blog › how-to-use-the-postgres-docker-official-image
How to Use the Postgres Docker Official Image | Docker
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
Published November 6, 2024
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 19 users
Languages Shell
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
Is there way to update my sql script and then update existing image, without data loss? for example I need to add trigger for existing postgres image... Hi, initdb.sh scripts are called only if there is no database set, so if you need to migrate current database it's better to run this sql migrations from psql cli for already created databases, and initdb.sh/ scripts will be run for new clean instances. ... FYI: do not pass the host (localhost) in the initialization script or you'll likely to encounter an error about not being able to connect to database. ... in docker-compose.yml patch /file.sql:/patch/to/docker-entrypoint-initdb.d/ in fact, point the file to the directory hint: remember where the work dir is base on your docker file for the image you are using to compose this container volumes:
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.
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.
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/
Linux Hint
linuxhint.com › run_postgresql_docker_compose
Running PostgreSQL using Docker Compose – Linux Hint
You will notice that the volume has a rather unfriendly name and is mounted at /var/lib/postgresql/data. Let’s remove this container and the associated volume for now: $ docker rm -f mydb $ docker volume rm 8328940661c0703ed867b004ea6343b9432e70069280b71cfce592ecdd12e55d · The same is true when you create a container using a simple docker-compose file.
Medium
medium.com › @jewelski › quickly-set-up-a-local-postgres-database-using-docker-5098052a4726
Quickly set up a local postgres database using docker | by jewelski | Medium
December 28, 2022 - You can use the POSTGRES_PASSWORD environment variable that you set in the Docker Compose file. Click the Save button to create the server. That’s it! Your database should now be up and running, and you should be able to connect to it using pgAdmin or any other PostgreSQL client. To verify that the database is working correctly, you can try running a few simple queries. For example, you can create a table and insert some data into it, and then run a SELECT query to retrieve the data: