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
🌐
Docker
docs.docker.com › guides › pre-seeding database with schema and data at startup for development environment
Pre-seeding database with schema and data at startup for development environment | Docker Docs
3 weeks ago - If you containerize the service that connects to the DB, you should connect to the database over a custom bridge network. Bring up the Compose service. Assuming that you've placed the seed.sql file in the same directory as the Dockerfile, execute the following command: ... It’s time to verify if the table users get populated with the data. $ docker exec -it my_postgres_db psql -h localhost -U postgres sampledb
Discussions

Using docker-compose to create tables in postgresql database - Stack Overflow
I am using docker-compose to deploy a multicontainer python Flask web application. I'm having difficulty understanding how to create tables in the postgresql database during the build so I don't have to add them manually with psql. More on stackoverflow.com
🌐 stackoverflow.com
docker compose postgres empty two database same container
The official docker image does not support that out of the box. You can either: create a shell script to run the "create database" sql command for the other "db" run 2 postgres containers in paralel More on reddit.com
🌐 r/docker
5
5
April 12, 2022
Where is Postgres database located when using Docker Compose (Immich)
Where do i delete old postgres files, I'm trying to start from scratch and now cant compose up. Ive tried to remove REDIS install and IMMICH etc, made new folder for Immich. i figured it was database not actually being deleted. More on reddit.com
🌐 r/unRAID
8
3
August 22, 2023
How to create postgres superuser role and DB on docker-compose.yml
Did u even Google this? Sheesh https://stackoverflow.com/questions/56249917/how-to-give-a-postgres-user-superuser-previllege-through-docker-compose More on reddit.com
🌐 r/docker
13
4
November 23, 2022
🌐
Medium
medium.com › @asuarezaceves › initializing-a-postgresql-database-with-a-dataset-using-docker-compose-a-step-by-step-guide-3feebd5b1545
Initializing a PostgreSQL Database with a Dataset using Docker Compose: A Step-by-step Guide | by Asuarezaceves | Medium
June 25, 2023 - This approach provides a reproducible and portable way of setting up databases, ideal for development, testing, and continuous integration environments. In your docker-compose.yml file, add this extra bit: version: '3.8' services: postgres: image: postgres:latest env_file: - .env volumes: - ./data:/var/lib/postgresql/data networks: - initexample metabase: image: metabase/metabase:latest depends_on: - postgres ports: - 3000:3000 networks: - initexample networks: initexample: driver: bridge
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
The postgres database is a default database meant for use by users, utilities and third party applications. ... $ docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres psql (14.3) Type "help" for help. postgres=# SELECT 1; ?column? ---------- 1 (1 row) Copy ... # Use postgres/example user/password credentials services: db: image: postgres restart: always # set shared memory limit when using docker compose ...
🌐
Warp
warp.dev › terminus by warp › docker › launch postgresql using docker compose
Launch PostgreSQL Using Docker Compose | Warp
April 5, 2024 - You can also read our other article on how to mount files in Compose. To initialize the database with a shell script or a file containing SQL instructions, for example to insert dummy data or migrate an existing database, you can place your \.sh and \.sql files within a directory on your local machine and mount it to the /docker-entrypoint-initdb.d directory using the volumes field as follows: ... version: '3' services: db: image: postgres environment: POSTGRES_USER: <username> POSTGRES_PASSWORD: <password> POSTGRES_DB: <database> volumes: - ./data:/var/lib/postgresql/data - ./init-scripts:/docker-entrypoint-initdb.d ports: - 5432:5432
🌐
Blog
blog.cadumagalhaes.dev › how-to-setup-a-postgresql-database-with-docker-compose
How to setup a PostgreSQL database with Docker Compose
February 5, 2025 - All you need to do is define your services, do some setups and run it. version: '3.8' services: postgres: image: postgres:15 environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=password volumes: - postgres-db-volume:/var/lib/postgresql/data ...
🌐
GitConnected
levelup.gitconnected.com › creating-and-filling-a-postgres-db-with-docker-compose-e1607f6f882f
Creating and filling a Postgres DB with Docker compose | by José David Arévalo | Level Up Coding
April 14, 2022 - In this case, we mapped our local port 5438 to the port 5432 (Postgres default port) inside the container., also we will persist the data in our machine, preventing data loss when deleting the containers, for this, we add the volume using the folder postgres-data as the place where Postgres data are stored. To create the tables in our database we need to copy our create_tables.sql script to/docker-entrypoint-initdb.d/
Find elsewhere
🌐
DEV Community
dev.to › saiful7778 › setting-up-postgresql-with-docker-compose-for-development-and-production-45j8
Setting Up PostgreSQL with Docker Compose for Development and Production - DEV Community
November 29, 2025 - -- ---------- 1. Create roles ---------- DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_migrator') THEN CREATE ROLE db_migrator LOGIN PASSWORD '12345678'; END IF; IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_rw') THEN CREATE ROLE db_rw LOGIN PASSWORD '12345678'; END IF; IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'db_ro') THEN CREATE ROLE db_ro LOGIN PASSWORD '12345678'; END IF; END $$; -- ---------- 2. Create databases ---------- CREATE DATABASE test; -- ============================================================ -- Apply permissions for each database
🌐
Medium
medium.com › @seventechnologiescloud › local-postgresql-database-via-docker-compose-the-ultimate-guide-dc80320fec5f
Local PostgreSQL Database via Docker compose: The ULTIMATE GUIDE! | by Seven Technologies Cloud | Medium
February 2, 2024 - To begin you’ll want to create your docker compose file, which are in essence instructions for how your docker container will be built. ... version: "3.9" services: db: image: postgres:13.4 restart: always environment: POSTGRES_USER: <your_user_here> POSTGRES_PASSWORD: <your_user_password_here> POSTGRES_DB: <your_database_schema_here> ports: - "5432:5432" volumes: - "./<your_sql_file_name_here>.sql:/docker-entrypoint-initdb.d/1.sql"
🌐
DEV Community
dev.to › 1cadumagalhaes › how-to-setup-a-postgresql-database-with-docker-compose-1j57
How to setup a PostgreSQL database with Docker Compose - DEV Community
September 22, 2023 - All you need to do is define your services, do some setups and run it. version: '3.8' services: postgres: image: postgres:15 environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=password volumes: - postgres-db-volume:/var/lib/postgresql/data ...
🌐
HostMyCode
hostmycode.com › home › tutorials › setting up postgresql 16 using docker compose
Setting Up PostgreSQL 16 Using Docker Compose | HostMyCode
August 22, 2024 - PostgreSQL is a powerful, open-source relational database system, widely used for its robustness and flexibility. 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.
Top answer
1 of 3
78

It didn't work for me with the COPY approach in Dockerfile. But I managed to run my init.sql file by adding the following to docker-compose.yml:

    volumes:
        - ./init.sql:/docker-entrypoint-initdb.d/init.sql

init.sql was in the same directory as my docker-compose.yml. I picked the solution from this gist. Check this article for more information.

2 of 3
62

I dont want to have to enter psql in order to type in

You can simply use container's built-in init mechanism:

COPY init.sql /docker-entrypoint-initdb.d/10-init.sql

This makes sure that your sql is executed after DB server is properly booted up.

Take a look at their entrypoint script. It does some preparations to start psql correctly and looks into /docker-entrypoint-initdb.d/ directory for files ending in .sh, .sql and .sql.gz.

10- in filename is because files are processed in ASCII order. You can name your other init files like 20-create-tables.sql and 30-seed-tables.sql.gz for example and be sure that they are processed in order you need.

Also note that invoking command does not specify the database. Keep that in mind if you are, say, migrating to docker-compose and your existing .sql files don't specify DB either.

Your files will be processed at container's first start instead of build stage though. Since Docker Compose stops images and then resumes them, there's almost no difference, but if it's crucial for you to init the DB at build stage I suggest still using built-in init method by calling /docker-entrypoint.sh from your dockerfile and then cleaning up at /docker-entrypoint-initdb.d/ directory.

🌐
DevOps.dev
blog.devops.dev › how-to-create-and-populate-a-postgres-db-with-docker-compose-293ad69649f2
How to Create and Populate a Postgres DB with Docker Compose | by Rehmanabdul | Medium | DevOps.dev
August 12, 2024 - This command would scale the app service to three instances, although it’s worth noting that scaling the database itself would require more advanced configurations such as replication or clustering. If you need to change configurations, such as increasing memory limits or adding new environment variables, simply update the docker-compose.yml file and restart the service: ... This will apply the new configurations while keeping your data intact. In this guide, we’ve walked through the steps to create and populate a PostgreSQL database using Docker Compose.
🌐
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 - The .env file defines the 3 environment ... in the docker compose file dc-postgresql-single.yml : POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD, which respectively corresponds to the name of a PostgreSQL Database, the name of a user of this database and the password of this user. The Postgres instance will be created with this ...
🌐
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 18 users
Languages   Shell
🌐
DevOps.dev
blog.devops.dev › how-to-create-and-fill-a-postgres-db-with-docker-compose-c145b5d7a0cd
How to Create and Fill a Postgres DB with Docker Compose? | by Usama Malik | Medium | DevOps.dev
November 14, 2024 - This guide will discuss the steps to set up a PostgreSQL database using Docker Compose, from creating the configuration file to populating the database with sample data.
🌐
Medium
dsysd-dev.medium.com › setting-up-your-own-postgres-database-using-docker-container-b46e5ee22d3a
Setting up your own postgres database using docker container | by dsysd dev | Medium
December 29, 2023 - Your Docker Compose file sets up a PostgreSQL database using the official PostgreSQL Docker image. It also includes a volume mount for initializing the database with an SQL script and uses a custom network.
🌐
GitHub
gist.github.com › onjin › 2dd3cc52ef79069de1faa2dfd456c945
example docker compose for postgresql with db init script · GitHub
When I get a shell into my db container, the database rivers was created but is empty, with no tables, relations, or data from my batched inserts. 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
🌐
GitHub
github.com › felipewom › docker-compose-postgres
GitHub - felipewom/docker-compose-postgres: Docker-Compose with PostgreSQL for local development ready to use · GitHub
May 16, 2026 - This tutorial teaches you how to create a Postgres Docker Compose file. ... PostgreSQL is one of the most used database engines nowadays.
Starred by 213 users
Forked by 52 users
🌐
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 ... · To launch your Postgres database and supporting services, enter the docker compose -f [FILE NAME] up command....