From 4b2c440146cdd4f438509b1eddbde3daea79e72d Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 04:39:19 +0800 Subject: [PATCH] add init database --- docker-compose.yml | 1 + .../docker-entrypoint-initdb.d/.gitignore | 1 + .../createdb.sh.example | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 postgres/docker-entrypoint-initdb.d/.gitignore create mode 100644 postgres/docker-entrypoint-initdb.d/createdb.sh.example diff --git a/docker-compose.yml b/docker-compose.yml index 93e28ddd..1b806b41 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -322,6 +322,7 @@ services: build: ./postgres volumes: - ${DATA_PATH_HOST}/postgres:/var/lib/postgresql/data + - ${POSTGRES_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d ports: - "${POSTGRES_PORT}:5432" environment: diff --git a/postgres/docker-entrypoint-initdb.d/.gitignore b/postgres/docker-entrypoint-initdb.d/.gitignore new file mode 100644 index 00000000..c97f963b --- /dev/null +++ b/postgres/docker-entrypoint-initdb.d/.gitignore @@ -0,0 +1 @@ +*.sh diff --git a/postgres/docker-entrypoint-initdb.d/createdb.sh.example b/postgres/docker-entrypoint-initdb.d/createdb.sh.example new file mode 100644 index 00000000..33de2ab9 --- /dev/null +++ b/postgres/docker-entrypoint-initdb.d/createdb.sh.example @@ -0,0 +1,34 @@ +#!/bin/bash +# +# Copy createdb.sh.example to createdb.sh +# then uncomment then set database name and username to create you need databases +# +# example: .env POSTGRES_USER=appuser and need db name is myshop_db +# +# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL +# CREATE USER $POSTGRES_USER; +# CREATE DATABASE myshop_db; +# GRANT ALL PRIVILEGES ON DATABASE myshop_db TO $POSTGRES_USER; +# EOSQL +# +# this sh script will auto run when the postgres container starts and the $DATA_PATH_HOST/postgres not found. +# + +set -e +# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL +# CREATE USER db1; +# CREATE DATABASE db1; +# GRANT ALL PRIVILEGES ON DATABASE db1 TO db1; +# EOSQL + +# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL +# CREATE USER db2; +# CREATE DATABASE db2; +# GRANT ALL PRIVILEGES ON DATABASE db2 TO db2; +# EOSQL + +# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL +# CREATE USER db3; +# CREATE DATABASE db3; +# GRANT ALL PRIVILEGES ON DATABASE db3 TO db3; +# EOSQL