0

I would like to add a DB, table and prepopulate with some kind of init.sql file. not sure what the init.sql file looks like. Also not sure where I would be putting the ADD command.

This docker-compose.yml file works

version: '3.6'
services:
  postgre:
    image: postgres:11.1-alpine
    ports:
      - "5432"
    environment:
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
      POSTGRESS_DB: 'db_amex01'
    volumes:
      - ./init:/docker-entrypoint-initdb.d/
IrishGringo
  • 3,864
  • 7
  • 37
  • 49

1 Answers1

0

You can add *.sh script(s) and/or *.sql instructions to the /docker-entrypoint-initdb.d/ directory (basically how you have in your docker-compose.yml file already).

An example of a shell script to create a user:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

You can generate your .sql file from an existing database by "dumping" the data with psql -U <username> -d <dbname> -1 -f <filename>.sql. You can then add the .sql file to your ./init/ directory.

This is documented at https://github.com/docker-library/docs/tree/master/postgres#how-to-extend-this-image.

yomateo
  • 2,078
  • 12
  • 17