Docker Community Forums

Share and learn in the Docker community.

Postgres library /docker-entrypoint-initdb.d seeem doesn't work


Hey guys,

With my Flask app I’m using the official postgres image from here:

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. I’m creating my container with docker-compose.yml:

    version: "3"
    image: postgres:10
    env_file: .env
    - 5432
    build: .
      - "5000:5000"
      - ./app/:/home/app/
      - db

I’m using a script inside the root/docker-entrypoint-initdb.d directory:

#!/usr/bin/env bash

psql -v ON_ERROR_STOP=1 --username "postgres" --dbname "testdb" <<-EOSQL
    CREATE SCHEMA tschema;
    CREATE TABLE tschema.test(firstname CHAR(15), lastname CHAR(20));
    INSERT INTO tschema.test values ('one', 'success');

Did I place it in the correct directory? or no directory needed?

So the thing is, the database named testdb gets created after run “docker-compose”, everything works fine, but the database doesn;t get populated with my data from <<-EOSQL…EOSQL shown above.

Any ideas what might be wrong? Ah… and by the way, here’s my .env file with enviroment variables that is used in the .yml file:


Thanks :slight_smile:


I had the same issue and I resolved it by creating a shell script to populate db.

First, I initialized db creating user and database using 01-init.sql
Then, I populated database using a shell script( Following is the shell script
psql -U user -d db < /dump/db-dump.sql

Scripts are executed in order of the name. So make sure to name them in order. You can name them simply as 01-script1, 02-script2…