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.

Answer from Victor Di on Stack Overflow
🌐
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
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
The default postgres user and database are created in the entrypoint with initdb. 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 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
Discussions

Using docker-compose to create tables in postgresql database - Stack Overflow
Note: Sometimes you'll need a sleep ... /etc/init.d/postgresql start line. In my experience either the init script or the psql client handles this for you, but I know that's not the case with mysql, so I thought I'd call it out. ... Thanks for the help. I've run into this error when building the docker-compose.y... More on stackoverflow.com
🌐 stackoverflow.com
How to run a script PostgreSQL with Docker Compose in the initialization ?
The Docker way would be to simply spin up a second container with the dev database. services: prod: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_prod:/postgres/var dev: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_dev:/postgres/var volumes: pg_prod: pg_dev: More on reddit.com
🌐 r/docker
6
1
June 20, 2024
Specify an initialization script for Postgres, using docker run
User an absolute path to init.sql, not ./init.js, e.g. ${PWD}/init.js. More on reddit.com
🌐 r/docker
2
3
July 21, 2021
docker-compose and create db in Postgres on init - Stack Overflow
In my case, I didn't declare a volume to /var/lib/postgresql/data, but it always has been created a volume in /var/lib/docker/volumes/, and the data persisted after stop and start the container. So, I forced the recreation of anonymous volumes. ... With that, you won't need to empty the database_data directory and run again docker-compose up every single time. ... This doesn't seem to work anymore. Either do docker compose down --volumes or create a custom init bash script ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
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
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.

🌐
Reddit
reddit.com › r/docker › how to run a script postgresql with docker compose in the initialization ?
r/docker on Reddit: How to run a script PostgreSQL with Docker Compose in the initialization ?
June 20, 2024 -

I have this:

services:
  db:
    image: postgres:16.3-alpine3.20
    restart: always
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 
      POSTGRES_DB: pento
    volumes:
      - ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
      - pg_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    
volumes:
  pg_data:

And I would like to CREATE other DATABASE when container is starting using a sql script.

For example:

/init-db.sql
CREATE DATABASE pento_dev;

How can I do that ? Is it possible ?

Find elsewhere
🌐
Reddit
reddit.com › r/docker › specify an initialization script for postgres, using docker run
r/docker on Reddit: Specify an initialization script for Postgres, using docker run
July 21, 2021 -

I there a way to specify an initialization script for Postgres, using docker run?

If I use Docker Compose, this line will copy the script and run it:

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

However, this syntax doesn't work in the CLI:

$ docker volume create postgres-data
$ docker run --detach \
	--rm \
	--name postgres \
	--publish 5432:5432 \
	--env-file .env \
	--volume postgres-data:/var/lib/postgresql/data \
	--volume ./init.sql:/docker-entrypoint-initdb.d/init.sql \
	postgres:9.6.22

docker: Error response from daemon: create ./init.sql: "./init.sql" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.

Is this possible?

🌐
Medium
medium.com › @aedemirsen › execute-sql-commands-at-postgresql-db-startup-with-docker-2be0abadec48
Execute Sql Commands At Postgresql DB Startup With Docker | by Ahmet Emre DEMİRŞEN | Medium
January 17, 2026 - Let’s create the following docker-compose.yml file to quickly create the database: version: "3.8" services: postgresql: image: postgres container_name: postgres_db restart: always environment: POSTGRES_DB: ${POSTGRES_DB} POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_ROOT_PASSWORD: ${POSTGRES_ROOT_PASSWORD} ports: - ${POSTGRES_PORT}:5432 volumes: - ./postgres/data:/var/lib/postgresql/data - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql ·
🌐
Mkyong
mkyong.com › home › docker › how to run an init script for docker postgres
How to run an init script for Docker Postgres - Mkyong.com
September 22, 2023 - To run the init script, copy the `*.sql` to the Docker Postgres container `/docker-entrypoint-initdb.d` directory.
🌐
Warp
warp.dev › terminus › postgres-docker-compose
Warp: Launch PostgreSQL Using Docker Compose
April 5, 2024 - You can also read our other article ... 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: Once the user and the database are created, postgres will run any ...
🌐
Restack
restack.io › p › postgresml-answer-docker-compose-init-script-cat-ai
Postgresml Docker Compose Init Script | Restackio
May 1, 2025 - Create a docker-compose.yml file in your project directory. This file will define the PostgreSQL service and any necessary configurations. Here’s an example: version: '3.8' services: postgres: image: postgres:latest restart: always environment: POSTGRES_USER: your_user POSTGRES_PASSWORD: your_password POSTGRES_DB: your_database volumes: - ./init-scripts:/docker-entrypoint-initdb.d ports: - "5432:5432"
🌐
ExitCode0
exitcode0.net › posts › automating postgres deployment with docker compose and init scripts
Automating Postgres Deployment with Docker Compose and Init Scripts | ExitCode0
January 6, 2024 - In this blog post, we will explore how to run PostgreSQL in a Docker container using Docker Compose. We will also break down and explain the init-user-db.sh script that is executed at startup to initialize the PostgreSQL tables. Running PostgreSQL in a Docker container provides several benefits, including ease of deployment, portability, and isolation.
🌐
Cybertec-postgresql
til.cybertec-postgresql.com › tags › docker-compose
Posts tagged "docker-compose"
postgres: image: postgres volumes: - ./init.sql:/docker-entrypoint-initdb.d/init.sql · Multiple scripts run in alphabetical order, thus it's good practice to prefix them with a number. volumes: - ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql - ./data.sql:/docker-entrypoint-initdb.d/2-data.sql · volumes: - ./init-scripts:/docker-entrypoint-initdb.d · You can specify one or multiple config files for docker-compose with the -f flag:
🌐
Medium
onexlab-io.medium.com › docker-compose-postgres-initdb-ba0021deef76
Docker Compose Postgres initdb. In this article, you will learn how to… | by Onexlab | Medium
July 30, 2023 - Docker Compose Postgres initdb In this article, you will learn how to do Postgres seeding using Docker Compose. Video Link Onexlab Udemy Let’s Start Creating Docker Seeding Hope you are familiar …
🌐
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/
🌐
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 - It gives you a clean environment, consistent versions, and a quick setup for both development and production. In this guide, we will walk step by step through a practical setup using Docker Compose. This setup includes: A custom PostgreSQL configuration · Automatic initialisation scripts ·
🌐
Docker Community
forums.docker.com › general
How to copy script to container inside a docker-compose.yml - General - Docker Community Forums
January 9, 2022 - Hello All , My working environment is windows10, wsl2, ubuntu 20.04 LTS Docker Desktop set for wsl2 I wanted to run sql scripts right from a docker-compose.yml in order to populate a new postgres DB.None of the examples I found worked . Finally I solved my issue following an excellent post by Mr gforghetti(How to runnig sql script file - #3 by gforghetti).Since the topic has been closed for 3 years my question is the following , Is there now a way to include an initial sql script executio...