Postgres only initializes the database if no database is found, when the container starts. Since you have a volume mapping on the database directory, chances are that a database already exists.

If you delete the db_data volume and start the container, postgres will see that there isn't a database and then it'll initialize one for you using the scripts in docker-entrypoint-initdb.d.

Answer from Hans Kilian on Stack Overflow
🌐
GitHub
github.com › docker-library › postgres › issues › 693
Initialization script not being executed · Issue #693 · docker-library/postgres
February 27, 2020 - When I login to the instance, the file /usr/local/bin/docker-entrypoint.sh is indeed there and the script I'm passing is in the /docker-entrypoint-initdb.d/ folder. drwxrwx--- 2 1000 1000 4096 Feb 27 10:47 . drwxr-xr-x 1 root root 4096 Feb 27 13:17 .. -rwxrwx--- 1 1000 1000 505 Feb 27 11:16 init.sql ... # docker-compose.yml version: '3' services: db: image: postgres:12.1-alpine container_name: pgsql restart: always volumes: - ./data/:/import_data/ - ./scripts/:/docker-entrypoint-initdb.d/ - /tmp/pgdata:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: example ports: - 5432:5432
Author   hugo-paredes
🌐
GitHub
github.com › docker-library › postgres › issues › 1373
PostgreSQL 18: Init scripts in /docker-entrypoint-initdb.d/ never execute due to automatic version subdirectory creation · Issue #1373 · docker-library/postgres
October 16, 2025 - Option 1: Modify docker-entrypoint.sh to check if the data directory contains only the version subdirectory (e.g., 18/) and the data symlink, and treat it as "empty" for initialization purposes.
Author   JoMiPeCa
Discussions

Docker entrypoint-initdb.d postgres
Have you ensured the data directory is empty? It might be using an anonymous volume, hence it's not running the init scripts, at least that's what the official docs says (Initialization Scripts section). Here's a quote: Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty. Which seems inline with the current logic in the entrypoint file: https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh#L313-L341 . Perhaps you can try to confirm whether line 339 in the link I shared, which says "skipping initialization" exists in your postgres logs when it doesn't work. More on reddit.com
🌐 r/docker
12
4
November 20, 2021
psql -h localhost fails in init script
psql -h localhost fails in init scripts. I encountered this problem while running a third party script I can not change. Simple Dockerfile to reproduce: FROM postgres:9.4 COPY ./init.sh /docker-ent... More on github.com
🌐 github.com
7
August 3, 2018
docker-entrypoint.sh ignoring my init scripts
Docker Official Image packaging for Postgres. Contribute to docker-library/postgres development by creating an account on GitHub. More on github.com
🌐 github.com
29
September 2, 2016
postgresql - Scripts in the /docker-entrypoint-initdb.d folder are ignored - Stack Overflow
In my case, there was a typoo and after fixing it, it worked fine after removing the volumes. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 1 Docker PostgreSQL initialisation script laced in docker-entrypoint-initdb.d’ fails to ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Docker Community
forums.docker.com › docker desktop
Initial scripts not being copied to docker-entrypoint-initdb.d folder - Docker Desktop - Docker Community Forums
January 19, 2019 - Hello, I am trying to create a new postgres db and running an initial script. These are the files I have so far. My own script is not being copied. version: ‘3’ services: db: image: postgres:11 volumes: - ./db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d ports: - ‘5432:5432’ #!/bin/bash set -e psql -v ON_ERROR_STOP=1 -U “$POSTGRES_USER”
🌐
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 - Access the running container and display the data inserted by init.sql. ... docker exec -it pg-container-name psql -U mkyong -d mydb psql (15.4 (Debian 15.4-1.pgdg120+1)) Type "help" for help. mydb=# select * from students; id | name | age ----+--------+----- 1 | Mkyong | 40 2 | Ali | 28 3 | Teoh | 18 (3 rows) mydb=# Exit the terminal. ... P.S. If the PostgreSQL container fails to start, we can use docker logs container-name to check the errors.
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts. For example, to add an additional user and database, add the following to /docker-entrypoint-initdb.d/init-user-db.sh: #!/usr/bin/env bash set -e psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL CREATE USER docker; CREATE DATABASE docker; GRANT ALL PRIVILEGES ON DATABASE docker TO docker; EOSQL Copy
🌐
GitHub
github.com › docker-library › postgres › issues › 474
psql -h localhost fails in init script · Issue #474 · docker-library/postgres
August 3, 2018 - psql -h localhost fails in init scripts. I encountered this problem while running a third party script I can not change. Simple Dockerfile to reproduce: FROM postgres:9.4 COPY ./init.sh /docker-entrypoint-initdb.d/ RUN chmod a+x /docker-entrypoint-initdb.d/init.sh ·
Author   simonseyock
Find elsewhere
🌐
GitHub
github.com › docker-library › postgres › issues › 193
docker-entrypoint.sh ignoring my init scripts · Issue #193
September 2, 2016 - questionUsability question, not directly related to an error with the imageUsability question, not directly related to an error with the image ... . ├── Dockerfile ├── docker-entrypoint-initdb.d │ └── init-user-db.sh └── test.sh · However the for f in /docker-entrypoint-initdb.d/*; do step in the docker-entrypoint.sh file seems to be incorrectly iterating over the contents of docker-entrypoint-initdb.d. ... # This works for f in docker-entrypoint-initdb.d/*; do echo "$f" done echo "---" # This is in docker-entrypoint.sh for f in /docker-entrypoint-initdb.d/*; do echo "$f" done
Author   tommyp1ckles
Top answer
1 of 4
22

Postgres only initializes the database if no database is found, when the container starts. Since you have a volume mapping on the database directory, chances are that a database already exists.

If you delete the db_data volume and start the container, postgres will see that there isn't a database and then it'll initialize one for you using the scripts in docker-entrypoint-initdb.d.

2 of 4
3

The accepted answer was correct (when it was written)

There was a subsequent discussion in github with the maintainer of the postgres docker image about supporting a mechanism similar to the mysql /always-init.d/ etc.

The link to that discussion: https://github.com/docker-library/postgres/pull/496

The solution with docker is to provide a custom entry-point, there is a bit of a bug on version shared in issue 496 so I'm posting a more updated version here in hopes others will find it useful:

#!/usr/bin/env bash
## copied from: https://github.com/docker-library/postgres/pull/496#issue-358838955
set -Eeo pipefail

echo " custom-entry-point"
# Example using the functions of the postgres entrypoint to customize startup to always run files in /always-initdb.d/

source "$(which docker-entrypoint.sh)"

docker_setup_env
docker_create_db_directories
# assumption: we are already running as the owner of PGDATA

# This is needed if the container is started as `root`
#if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
if [ "$(id -u)" = '0' ]; then
  exec gosu postgres "$BASH_SOURCE" "$@"
fi


if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
  echo " db is missing"
  docker_verify_minimum_env
  docker_init_database_dir
  pg_setup_hba_conf

  # only required for '--auth[-local]=md5' on POSTGRES_INITDB_ARGS
  export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"

  docker_temp_server_start "$@" -c max_locks_per_transaction=256
  docker_setup_db
  docker_process_init_files /docker-entrypoint-initdb.d/*
  docker_temp_server_stop
else
  echo " db already exists"
  docker_temp_server_start "$@"
  docker_process_init_files /always-initdb.d/*
  docker_temp_server_stop
fi

echo " .. starting!"
exec postgres "$@"
🌐
GitHub
gist.github.com › onjin › 2dd3cc52ef79069de1faa2dfd456c945
example docker compose for postgresql with db init script · GitHub
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: ... well what i did which worked was that i deleted all previous containers of the same image. docker rm -f -v postgres_container_name.
🌐
Reddit
reddit.com › r/docker › postgres init script
r/docker on Reddit: Postgres init script
April 12, 2025 -

I have a standard postgres container running, with the pg_data volume mapped to a directory on the host machine.

I want to be able to run an init script everytime I build or re-build the container, to run migrations and other such things. However, any script or '.sql' file placed in /docker-entrypoint-initdb.d/ only gets executed if the pg_data volume is empty.

What is the easiest solution to this – at the moment I could make a pg_dump pf the pg_data directory, then remove it’s content, and restore from the pg_dump, but it seems pointlessly convoluted and open to errors with potential data loss.

🌐
GitHub
github.com › docker-library › postgres › issues › 40
postgres /docker-entrypoint.sh not running scripts in /docker-entrypoint-initdb.d/ · Issue #40 · docker-library/postgres
December 13, 2014 - It looks like /docker-entrypoint.sh is supposed to source any scripts it finds in /docker-entrypoint-initdb.d/. I verified I'm running this version of the script by cating the script and verifying this is the expected behavior. I followed the instructions in "justfalter"'s comment here: https://registry.hub.docker.com/_/postgres/ to add a fix-acl.sh file and put it in the correct folder.
Author   kevinburke
🌐
Stack Overflow
stackoverflow.com › questions › 74533746 › postgres-in-docker-compose-not-running-init-scripts
postgresql - Postgres in docker-compose not running init scripts - Stack Overflow
It might well not be re-running the init scripts if the db is already initialised. ... The log file answers this question directly: "PostgreSQL Database directory appears to contain a database; Skipping initialization" ... jjanes and Richard Huxton you were both correct. I removed the database but it needed a bounce of docker.socket/docker.service
🌐
CodeGenes
codegenes.net › blog › docker-postgres-does-not-run-init-file-in-docker-entrypoint-initdb-d
Docker Postgres: Troubleshooting init.sql Not Running in docker-entrypoint-initdb.d Directory — codegenes.net
When working with PostgreSQL in Docker, initializing databases, users, or schemas often relies on running SQL scripts during container startup. The official PostgreSQL Docker image simplifies this by executing scripts in the `/docker-entrypoint-initdb.d` directory **only during the first initialization** (when the database data directory is empty).
🌐
GitHub
github.com › bitnami › containers › issues › 16933
[bitnami/postgresql] Postgres init script no longer works after latest update · Issue #16933 · bitnami/containers
December 15, 2022 - Name and Version bitnami/postgresql:12 What steps will reproduce the bug? Create a container using a postgres init shell script that contains the following: psql -d template1 -c "CREATE EXTENSION pg_trgm;" Note that a password is request...
Author   wizardfrag
🌐
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?

🌐
GitHub
github.com › docker-library › postgres › issues › 941
Postgres not actually up when initialization script is run on a new container image, not accepting connections. · Issue #941 · docker-library/postgres
March 8, 2022 - You can now start the database server using: pg_ctl -D /var/lib/postgresql/data -l logfile start initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. waiting for server to start....2022-03-08 14:34:38.124 UTC [49] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit 2022-03-08 14:34:38.126 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5
Author   JeffGradyAtMolarCo
🌐
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 ?