EDIT - since Jul 23, 2015

The official postgres docker image will run .sql scripts found in the /docker-entrypoint-initdb.d/ folder.

So all you need is to create the following sql script:

init.sql

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

and add it in your Dockerfile:

Dockerfile

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/

But since July 8th, 2015, if all you need is to create a user and database, it is easier to just make use to the POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB environment variables:

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

or with a Dockerfile:

FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

for images older than Jul 23, 2015

From the documentation of the postgres Docker image, it is said that

[...] it will source any *.sh script found in that directory [/docker-entrypoint-initdb.d] to do further initialization before starting the service

What's important here is "before starting the service". This means your script make_db.sh will be executed before the postgres service would be started, hence the error message "could not connect to database postgres".

After that there is another useful piece of information:

If you need to execute SQL commands as part of your initialization, the use of Postgres single user mode is highly recommended.

Agreed this can be a bit mysterious at the first look. What it says is that your initialization script should start the postgres service in single mode before doing its actions. So you could change your make_db.ksh script as follows and it should get you closer to what you want:

NOTE, this has changed recently in the following commit. This will work with the latest change:

export PGUSER=postgres
psql <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

Previously, the use of --single mode was required:

gosu postgres postgres --single <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL
Answer from Thomasleveil 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
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
Now that you have learned how to launch Postgres and pre-seed the database using an SQL script, it’s time to learn how to mount an SQL file directly into the Postgres containers’ initialization directory (/docker-entrypoint-initdb.d).
Discussions

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
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
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
Postgres init script
For schema and data migrations, I wrote an init container that runs golang-migrate: https://github.com/golang-migrate/migrate More on reddit.com
🌐 r/docker
4
4
April 12, 2025
Top answer
1 of 10
654

EDIT - since Jul 23, 2015

The official postgres docker image will run .sql scripts found in the /docker-entrypoint-initdb.d/ folder.

So all you need is to create the following sql script:

init.sql

CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

and add it in your Dockerfile:

Dockerfile

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/

But since July 8th, 2015, if all you need is to create a user and database, it is easier to just make use to the POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB environment variables:

docker run -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker library/postgres

or with a Dockerfile:

FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

for images older than Jul 23, 2015

From the documentation of the postgres Docker image, it is said that

[...] it will source any *.sh script found in that directory [/docker-entrypoint-initdb.d] to do further initialization before starting the service

What's important here is "before starting the service". This means your script make_db.sh will be executed before the postgres service would be started, hence the error message "could not connect to database postgres".

After that there is another useful piece of information:

If you need to execute SQL commands as part of your initialization, the use of Postgres single user mode is highly recommended.

Agreed this can be a bit mysterious at the first look. What it says is that your initialization script should start the postgres service in single mode before doing its actions. So you could change your make_db.ksh script as follows and it should get you closer to what you want:

NOTE, this has changed recently in the following commit. This will work with the latest change:

export PGUSER=postgres
psql <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

Previously, the use of --single mode was required:

gosu postgres postgres --single <<- EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL
2 of 10
63

By using docker-compose:

Assuming that you have following directory layout:

$MYAPP_ROOT/docker-compose.yml
           /Docker/init.sql
           /Docker/db.Dockerfile

File: docker-compose.yml

version: "3.3"
services:
  db:
    build:
      context: ./Docker
      dockerfile: db.Dockerfile
    volumes:
      - ./var/pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

File: Docker/init.sql

CREATE USER myUser;

CREATE DATABASE myApp_dev;
GRANT ALL PRIVILEGES ON DATABASE myApp_dev TO myUser;

CREATE DATABASE myApp_test;
GRANT ALL PRIVILEGES ON DATABASE myApp_test TO myUser;

File: Docker/db.Dockerfile

FROM postgres:11.5-alpine
COPY init.sql /docker-entrypoint-initdb.d/

Composing and starting services:

docker-compose -f docker-compose.yml up --no-start
docker-compose -f docker-compose.yml start
🌐
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 - docker run --name pg-container-name -p 5432:5432 -e POSTGRES_USER=mkyong -e POSTGRES_PASSWORD=password -e POSTGRES_DB=mydb -v /path/to/init.sql:/docker-entrypoint-initdb.d/init.sql -d postgres
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.
🌐
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?

Find elsewhere
🌐
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 ·
🌐
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 ?

🌐
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 …
🌐
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.

🌐
Coding with Coffee
cadu.dev › creating-a-docker-image-with-database-preloaded
Creating a Docker image with a preloaded database | Coding with Coffee
January 29, 2021 - If we want to start a Postgresql Docker container with this dump loaded to share with our team, we can add this SQL file into the /docker-entrypoint-initdb.d/ folder inside the container, like explained into the Postgresql Image docs from DockerHub. Initialization scripts If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary).
🌐
w3tutorials
w3tutorials.net › blog › how-to-create-user-database-in-script-for-docker-postgres
How to Create User and Database in Docker Postgres Using init Scripts: Fixing Common Setup Errors — w3tutorials.net
Docker Postgres images are designed to run initialization scripts (SQL or shell) during the first startup, allowing you to automate user creation, database setup, and schema initialization.
🌐
DEV Community
dev.to › karanpratapsingh › seeding-postgres-with-docker-19n7
Seeding Postgres with Docker - DEV Community
November 12, 2021 - In the init.sh, we will just execute our *.sql script. #!/bin/bash psql -U $POSTGRES_USER -d $POSTGRES_DB -a -f /app/scripts/db/dump.sql · In the Dockerfile, we will copy our init.sh to docker-entrypoint-initdb.d directory.
🌐
DevPress
devpress.csdn.net › postgresql › 6304d2117e6682346619cc85.html
Initialize PostgreSQL Container with docker-entrypoint-initdb.d script_postgresql_Postgredaxiang-PostgreSQL
August 23, 2022 - I've connected to the running container and confirmed that the init.sql file is in place on the filesystem. Any idea what I could be doing wrong here? So from this Dockerfile I assume the user is postgress. ... FROM postgres:11.5 USER postgres RUN whoami ADD ./scripts/init.sql /docker-entrypoint-initdb.d/ ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 5432 CMD ["postgres"]
🌐
Docker
docs.docker.com › guides › postgresql › advanced configuration and initialization
Advanced Configuration and Initialization | Docker Docs
Use Docker Compose to mount initialization scripts into the container. First, create a project directory: $ mkdir -p postgres-project/init-db $ cd postgres-project
🌐
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
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
How to initialize GitLab docker postgres service with schema and data - GitLab CI/CD - GitLab Forum
March 5, 2018 - How do you initialize the postgres service on GitLab Docker CI? In a Dockerfile you copy .sql files to docker-entrypoint-initdb.d Example: FROM postgres:9.5.9 ... (other stuff here) COPY schemas.sql /docker-entrypoi…