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

    version: "3"
services:
  db:
    image: postgres:10
    env_file: .env
    expose:
    - 5432
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - ./app/:/home/app/
    depends_on:
      - db

I’m using a init-user-db.sh 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');
EOSQL

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:

POSTGRES_USER=postgres
POSTGRES_PASSWORD=123
POSTGRES_DB=testdb

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(02-import.sh). 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…