Docker Community Forums

Share and learn in the Docker community.

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


#1

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: