in volumes section you specify path to dirs not path to files

change

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

to

volumes:
   ./postgresql:/docker-entrypoint-initdb.d

or to

volumes:
   <path to dir with init.sql>:/docker-entrypoint-initdb.d

where <path to dir with init.sql> is propper local path

Answer from Ryabchenko Alexander on Stack Overflow
Discussions

postgresql - Create table and scheme postgres using docker compose - Stack Overflow
I am trying to create a postgres database with schema and table in it using docker- compose file. I have read some posts regarding this topic and tried the suggested solutions: Copying the .sql fi... More on stackoverflow.com
🌐 stackoverflow.com
Initial scripts not being copied to docker-entrypoint-initdb.d folder
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’ ... More on forums.docker.com
🌐 forums.docker.com
1
0
January 19, 2019
Postgress init file cannot be found when run in codefresh
Hi I have init.sql file in the same folder as docker-compose.yml, when it runs in Codefresh I get this error [2024-01-30T15:57:58.457Z] postgres | psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory locally it runs fine. any suggestions how to fix ... More on community.codefresh.io
🌐 community.codefresh.io
3
0
January 30, 2024
python - Simple Docker Postgres Can't Connect to Database - Stack Overflow
A simple docker postgres with Postgresql connecting to Python/Django can't connect to the database. Why isn't this working? Start script (here I am cleaning the initial set up as much as I can - all More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › mikecao › umami › issues › 527
[Troubleshooting] Could not read from input file: Is a directory · Issue #527 · umami-software/umami
March 8, 2021 - Having an issue with the latest Docker install. Steps to reproduce: Copy the docker-compose.yml from master docker-compose up -d The error is listed at the end of this log: db_1 | server started db_1 | CREATE DATABASE db_1 | db_1 | db_1 ...
Author   king-gheedorah
🌐
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”
Find elsewhere
🌐
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).
Top answer
1 of 4
17

Initialize Postgres container with Data

Create a docker-compose.yml

services:
  postgress-postgresql:
    image: postgres:bullseye
    container_name: postgres
    volumes:
      - postgresql_data:/var/lib/postgresql/data/
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      - POSTGRES_USER=postgress
      - POSTGRES_PASSWORD=MyStrongPassword123
    ports:
      - 5432:5432
    networks:
      - postgres
networks:
  postgres:
volumes:
  postgresql_data:

Create a init.sql with the script

 CREATE USER vbv WITH PASSWORD 'vbv';
 CREATE DATABASE vbvdb;
 GRANT ALL PRIVILEGES ON DATABASE vbvdb TO vbv;
 
 \connect vbvdb;
 
 CREATE TABLE employees (
     id SERIAL PRIMARY KEY,
     name VARCHAR(100),
     position VARCHAR(50),
     salary DECIMAL(10, 2)
 );
 
 INSERT INTO employees (name, position, salary) VALUES
 ('Bhuvi', 'Manager', 675000.00),
 ('Vibhu', 'Developer', 555000.00),
 ('Rudra', 'Analyst', 460000.00);
 
 GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO vbv;

RUN with docker-compose up -d

Connect to the container psql client - docker exec -it postgres bash. Check the data exists.

 ➜  ~ docker exec -it postgres bash
 root@96a599122941:/# psql -U vbv -d vbvdb
 psql (17.0 (Debian 17.0-1.pgdg110+1))
 Type "help" for help.
 
 vbvdb=> SELECT * FROM employees;
  id | name  | position  |  salary  
 ----+-------+-----------+----------
   1 | Bhuvi | Manager   | 675000.00
   2 | Vibhu | Developer | 555000.00
   3 | Rudra | Analyst   | 460000.00
 (3 rows)
 
 vbvdb=>

You can also validate docker logs -f postgres contains the following.

 2024-10-04 08:04:03.810 UTC [48] LOG:  database system is ready to accept connections
  done
 server started
 CREATE DATABASE
 
 
 /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
 CREATE ROLE
 CREATE DATABASE
 GRANT
 You are now connected to database "vbvdb" as user "postgress".
 CREATE TABLE
 INSERT 0 3
 GRANT

Added the activity logs into the gist https://gist.github.com/jinnabaalu/89bd8eeba3b8845cf337b85a807748f1

2 of 4
14

So from this Dockerfile I assume the user is postgress.

Try with this Dockerfile

FROM postgres:11.5
USER postgres
RUN whoami
ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]

update:

Seems like the file not owned by Postgres user.

Try to set permission

ADD ./scripts/init.sql /docker-entrypoint-initdb.d/
RUN chown postgres:postgres /docker-entrypoint-initdb.d/init.sql
🌐
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 - Scripts placed in /docker-entrypoint-initdb.d/ are never executed when using PostgreSQL 18 Docker image because the data directory is never considered "empty" due to automatic creation of version subdirectory.
Author   JoMiPeCa
🌐
Stack Overflow
stackoverflow.com › questions › 57548965 › sql-file-in-docker-entrypoint-initdb-not-being-run
postgresql - .sql file in docker-entrypoint-initdb not being run - Stack Overflow
Second thing, The above will work fine with initialization the DB but there is a mounting issue in the lastest image that might be the reason that the mount directory seemed empty. Also in the documentation, the path is -v /path/to/postgresql-persistence:/bitnami/postgresql Persisting your database ... Sign up to request clarification or add additional context in comments. ... 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 import file
🌐
Docker Community
forums.docker.com › docker desktop
Postgres library /docker-entrypoint-initdb.d seeem doesn't work - Docker Desktop - Docker Community Forums
December 5, 2018 - Hey guys, With my Flask app I’m using the official postgres image from here: https://hub.docker.com/_/postgres/ In the “How to extend this image” section it’s explained how I can execute additional SLQ commands to pre-populate my database with data. I’m actually having hard times doing it.
🌐
Stack Overflow
stackoverflow.com › questions › 74000155 › docker-issues-with-psql-database-and-init-sql-file
postgresql - Docker issues with psql database and init.sql file - Stack Overflow
services: postgres: image: postgres container_name: postgres ports: - "5432:5432" restart: unless-stopped hostname: postgres volumes: - ./app/db/init.sql:/docker-entrypoint-initdb.d/init.sql # - ./postgres/data:/var/lib/postgresql/data environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres # adminer: # image: adminer # restart: always # depends_on: # - postgres # # ports: # # - "8080:8080" # networks: # - docker app: container_name: app image: app depends_on: - postgres ports: - "8080:8080" build: ./app
🌐
GitHub
github.com › docker-library › postgres › issues › 693
Initialization script not being executed · Issue #693 · docker-library/postgres
February 27, 2020 - # 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 · -- ./scripts/init.sql -- Create a new database called 'moviesshows' CREATE TABLE Movies ( show_id INTEGER NULL, title VARCHAR (1024) NULL, director VARCHAR (1024) NULL, actors TEXT NULL, country VARCHAR (512) NULL, date_added VARCHAR (256) NULL, release_year SMALLINT NULL, rating VARCHAR (50) NULL, duration VARCHAR (128) NULL, listed_in VARCHAR (1024) NULL, plot TEXT NULL, which VARCHAR (50) NULL ); COPY movies FROM '/import_data/movies-shows.csv' delimiter ',' CSV HEADER;
Author   hugo-paredes
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 "$@"