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 Overflow
Discussions

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
🌐 r/docker
8
36
December 25, 2021
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.com
🌐 r/docker
11
5
June 22, 2018
Tricks 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
🌐 r/docker
4
87
September 30, 2019
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
🌐 r/docker
6
5
November 29, 2017
🌐
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
A Docker-Compose file with PostgreSQL ready to use. - gaelgthomas/docker-compose-with-postgresql
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%
🌐
Medium
medium.com › norsys-octogone › a-local-environment-for-postgresql-with-docker-compose-7ae68c998068
A local environment for PostgreSQL with Docker Compose | by Christophe Vaudry | norsys-octogone | Medium
February 16, 2026 - If you want to skip the details ...se-examples/postgresql-complete directory and run the following command : docker compose -f dc-postgresql-complete.yml up -d....
Find elsewhere
🌐
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:
🌐
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 ta…
🌐
DEV Community
dev.to › masanedevesh › docker-compose-quick-local-development-setup-postgresql-deo
Docker Compose: Quick Local Development Setup - Postgresql - DEV Community
March 3, 2024 - services: local-postgres-service: ... local-postgres-network: ... Start the container in detach mode using: docker compose -f postgres-local.yml up -d Stop the container using: docker compose -f postgres-local.yml ...
🌐
Baeldung
baeldung.com › home › spring › spring boot › running spring boot with postgresql in docker compose
Running Spring Boot with PostgreSQL in Docker Compose | Baeldung
4 weeks ago - Now let’s write our Docker Compose file, docker-compose.yml, and save it in src/main/docker: version: '2' services: app: image: 'docker-spring-boot-postgres:latest' build: context: .
🌐
HostMyCode
hostmycode.com › home › tutorials › setting up postgresql 16 using docker compose
Setting Up PostgreSQL 16 Using Docker Compose
August 22, 2024 - We'll show you how to define a docker-compose.yml file to set up the PostgreSQL service, automatically create a database, and insert predefined data upon initialization.
🌐
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
🌐
DevGenius
blog.devgenius.io › tips-and-tricks-to-using-postgresql-docker-image-80047673c9a5
Tips and Tricks to using PostgreSQL Docker Image | by Tony Oreglia | Dev Genius
January 2, 2022 - This article shares some tips and code examples for setting up your PostgreSQL database with Docker Compose and Flyway.
🌐
Hashinteractive
hashinteractive.com › blog › docker-compose-up-with-postgres-quick-tips
Docker Compose Up With Postgres Quick Tips | Hash Interactive
February 20, 2020 - We need a way to mount some volumes, add some configuration, and import/seed our database. Docker Compose to the rescue! Let’s setup a simple project structure: mkdir -p docker-postgres/data && cd docker-postgres && touch docker-compose.yml.
🌐
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.
🌐
DEV Community
dev.to › tienbku › docker-compose-spring-boot-and-postgres-example-4l82
Docker Compose: Spring Boot and Postgres example - DEV Community
October 14, 2023 - Create Spring Boot App working with Postgres database. Create Dockerfile for Spring Boot App. Write Docker Compose configurations in YAML file.
🌐
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: