From 4b2c440146cdd4f438509b1eddbde3daea79e72d Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 04:39:19 +0800 Subject: [PATCH 1/5] 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 From 999118b674761bb2e5d42a525023b762b923c7c2 Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 15:27:37 +0800 Subject: [PATCH 2/5] update example --- postgres/docker-entrypoint-initdb.d/createdb.sh.example | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/postgres/docker-entrypoint-initdb.d/createdb.sh.example b/postgres/docker-entrypoint-initdb.d/createdb.sh.example index 33de2ab9..680451d8 100644 --- a/postgres/docker-entrypoint-initdb.d/createdb.sh.example +++ b/postgres/docker-entrypoint-initdb.d/createdb.sh.example @@ -6,7 +6,7 @@ # 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 USER $POSTGRES_USER PASSWORD ; # CREATE DATABASE myshop_db; # GRANT ALL PRIVILEGES ON DATABASE myshop_db TO $POSTGRES_USER; # EOSQL @@ -16,19 +16,19 @@ set -e # psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL -# CREATE USER db1; +# CREATE USER db1 PASSWORD 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 USER db2 PASSWORD 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 USER db3 PASSWORD db3; # CREATE DATABASE db3; # GRANT ALL PRIVILEGES ON DATABASE db3 TO db3; # EOSQL From 75b3fa499ee587dfc39ff83b080e310b27b728dc Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 15:30:45 +0800 Subject: [PATCH 3/5] no message --- postgres/docker-entrypoint-initdb.d/createdb.sh.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/docker-entrypoint-initdb.d/createdb.sh.example b/postgres/docker-entrypoint-initdb.d/createdb.sh.example index 680451d8..28110231 100644 --- a/postgres/docker-entrypoint-initdb.d/createdb.sh.example +++ b/postgres/docker-entrypoint-initdb.d/createdb.sh.example @@ -6,9 +6,9 @@ # 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 PASSWORD ; +# CREATE USER my_user PASSWORD my_password; # CREATE DATABASE myshop_db; -# GRANT ALL PRIVILEGES ON DATABASE myshop_db TO $POSTGRES_USER; +# GRANT ALL PRIVILEGES ON DATABASE myshop_db TO my_user; # EOSQL # # this sh script will auto run when the postgres container starts and the $DATA_PATH_HOST/postgres not found. From 6caa4c5fb90976c2aa99f7553b29d474afbb39e0 Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 23:27:28 +0800 Subject: [PATCH 4/5] fix wrong sql --- .../docker-entrypoint-initdb.d/createdb.sh.example | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/postgres/docker-entrypoint-initdb.d/createdb.sh.example b/postgres/docker-entrypoint-initdb.d/createdb.sh.example index 28110231..8a5cf693 100644 --- a/postgres/docker-entrypoint-initdb.d/createdb.sh.example +++ b/postgres/docker-entrypoint-initdb.d/createdb.sh.example @@ -6,9 +6,9 @@ # 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 my_user PASSWORD my_password; +# CREATE USER myuser IDENTIFIED BY ENCRYPTED PASSWORD 'mypassword'; # CREATE DATABASE myshop_db; -# GRANT ALL PRIVILEGES ON DATABASE myshop_db TO my_user; +# GRANT ALL PRIVILEGES ON DATABASE myshop_db TO myuser; # EOSQL # # this sh script will auto run when the postgres container starts and the $DATA_PATH_HOST/postgres not found. @@ -16,19 +16,19 @@ set -e # psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL -# CREATE USER db1 PASSWORD db1; +# CREATE USER db1 IDENTIFIED BY ENCRYPTED PASSWORD '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 PASSWORD db2; +# CREATE USER db2 IDENTIFIED BY ENCRYPTED PASSWORD '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 PASSWORD db3; +# CREATE USER db3 IDENTIFIED BY ENCRYPTED PASSWORD 'db3'; # CREATE DATABASE db3; # GRANT ALL PRIVILEGES ON DATABASE db3 TO db3; # EOSQL From 7199ae5a340f5289e61982824ea7ee88bc9955ee Mon Sep 17 00:00:00 2001 From: "KUI\\ahkui" Date: Fri, 15 Jun 2018 23:37:27 +0800 Subject: [PATCH 5/5] fix wrong example --- postgres/docker-entrypoint-initdb.d/createdb.sh.example | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/postgres/docker-entrypoint-initdb.d/createdb.sh.example b/postgres/docker-entrypoint-initdb.d/createdb.sh.example index 8a5cf693..e94b46ee 100644 --- a/postgres/docker-entrypoint-initdb.d/createdb.sh.example +++ b/postgres/docker-entrypoint-initdb.d/createdb.sh.example @@ -6,7 +6,7 @@ # 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 myuser IDENTIFIED BY ENCRYPTED PASSWORD 'mypassword'; +# CREATE USER myuser WITH PASSWORD 'mypassword'; # CREATE DATABASE myshop_db; # GRANT ALL PRIVILEGES ON DATABASE myshop_db TO myuser; # EOSQL @@ -16,19 +16,19 @@ set -e # psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL -# CREATE USER db1 IDENTIFIED BY ENCRYPTED PASSWORD 'db1'; +# CREATE USER db1 WITH PASSWORD '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 IDENTIFIED BY ENCRYPTED PASSWORD 'db2'; +# CREATE USER db2 WITH PASSWORD '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 IDENTIFIED BY ENCRYPTED PASSWORD 'db3'; +# CREATE USER db3 WITH PASSWORD 'db3'; # CREATE DATABASE db3; # GRANT ALL PRIVILEGES ON DATABASE db3 TO db3; # EOSQL