From 84385cbf2ae59e6c4f1823ddb0f2c915a251360b Mon Sep 17 00:00:00 2001 From: Hrishikesh Date: Tue, 29 Jun 2021 01:46:54 +0530 Subject: [PATCH 1/6] Exists check for database and user - Added exists check for database and user - Added `--dbname "postgres"` in `psql` command - Added owner, utf8 encoding, collate and template --- create-multiple-postgresql-databases.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index aa665fa..8cedaf0 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -6,9 +6,9 @@ set -u function create_user_and_database() { local database=$1 echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL + SELECT 'CREATE USER ' || LOWER(TRIM('$database')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$database')));\gexec + SELECT 'CREATE DATABASE ' || LOWER(TRIM('$database')) || ' WITH OWNER "$POSTGRES_USER" ENCODING "UTF8" LC_COLLATE = "en_US.UTF-8" LC_CTYPE = "en_US.UTF-8" TEMPLATE="template0"' AS create_table_query WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = LOWER(TRIM('$database')));\gexec GRANT ALL PRIVILEGES ON DATABASE $database TO $database; EOSQL } From 6151b43cba194e0b321b040b04190fb36acb7174 Mon Sep 17 00:00:00 2001 From: Helio Chissini de Castro Date: Wed, 15 Dec 2021 21:29:27 +0100 Subject: [PATCH 2/6] Simple script improvement to add the capability of different user and password per database entry Signed-off-by: Helio Chissini de Castro --- README.md | 9 +++++---- create-multiple-postgresql-databases.sh | 15 +++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3db6ca3..58c4852 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ mechanism. ### By mounting a volume Clone the repository, mount its directory as a volume into -`/docker-entrypoint-initdb.d` and declare database names separated by commas in -`POSTGRES_MULTIPLE_DATABASES` environment variable as follows +`/docker-entrypoint-initdb.d` and declare database names separated by commas and each entry with database, user and password separated by double colon in `POSTGRES_MULTIPLE_DATABASES` environment variable as follows (`docker-compose` syntax): myapp-postgresql: @@ -27,9 +26,10 @@ Clone the repository, mount its directory as a volume into volumes: - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 + - POSTGRES_MULTIPLE_DATABASES=db1:user:pwd,db2:user:pwd - POSTGRES_USER=myapp - POSTGRES_PASSWORD= + - POSTGRES_DB=db ### By building a custom image @@ -48,10 +48,11 @@ to the container: - POSTGRES_MULTIPLE_DATABASES=db1,db2 - POSTGRES_USER=myapp - POSTGRES_PASSWORD= + - POSTGRES_DB=db ### Non-standard database names If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: environment: - - POSTGRES_MULTIPLE_DATABASES="test-db-1","test-db-2" + - POSTGRES_MULTIPLE_DATABASES="test-db-1:user:pwd","test-db-2:user:pwd" diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index aa665fa..b582883 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -4,12 +4,15 @@ set -e set -u function create_user_and_database() { - local database=$1 - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $database; + local dbinfo=$1 + IFS=":" read -r database user password <<< "$dbinfo" + + echo "Creating database '$database' with user '$user' and password '$password'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER $user; + ALTER USER $user WITH ENCRYPTED PASSWORD '$password'; + CREATE DATABASE $database; + GRANT ALL PRIVILEGES ON DATABASE $database TO $user; EOSQL } From 4f18650f855969535e3e96f9e88df6bd8d638b37 Mon Sep 17 00:00:00 2001 From: Corbin Bartsch Date: Fri, 24 Jun 2022 15:03:13 -0400 Subject: [PATCH 3/6] Update README with merge note --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 58c4852..1abecbc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Using multiple databases with the official PostgreSQL Docker image +This fork merges [heliocastro:user_pass_improvements](https://github.com/heliocastro/docker-postgresql-multiple-databases/tree/user_pass_improvement) + The [official recommendation](https://hub.docker.com/_/postgres/) for creating multiple databases is as follows: From 2c047bab97d6839ade176a8f25e26b2e96fd4e57 Mon Sep 17 00:00:00 2001 From: Corbin Bartsch Date: Fri, 24 Jun 2022 15:06:41 -0400 Subject: [PATCH 4/6] Keep user name same as database name --- create-multiple-postgresql-databases.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index 40c026f..132689e 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -5,14 +5,14 @@ set -u function create_user_and_database() { local dbinfo=$1 - IFS=":" read -r database user password <<< "$dbinfo" + IFS=":" read -r database password <<< "$dbinfo" echo " Creating user and database '$database'" echo "Creating database '$database' with user '$user' and password '$password'" psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL - SELECT 'CREATE USER ' || LOWER(TRIM('$user')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$user')));\gexec - ALTER USER $user WITH ENCRYPTED PASSWORD '$password'; + SELECT 'CREATE USER ' || LOWER(TRIM('$database')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$database')));\gexec + ALTER USER $database WITH ENCRYPTED PASSWORD '$password'; SELECT 'CREATE DATABASE ' || LOWER(TRIM('$database')) || ' WITH OWNER "$POSTGRES_USER" ENCODING "UTF8" LC_COLLATE = "en_US.UTF-8" LC_CTYPE = "en_US.UTF-8" TEMPLATE="template0"' AS create_table_query WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = LOWER(TRIM('$database')));\gexec - GRANT ALL PRIVILEGES ON DATABASE $database TO $user; + GRANT ALL PRIVILEGES ON DATABASE $database TO $database; EOSQL } From fec53d8488eafc8c40f96559f846bfe83bedff51 Mon Sep 17 00:00:00 2001 From: Corbin Bartsch Date: Fri, 24 Jun 2022 15:10:30 -0400 Subject: [PATCH 5/6] Update README.md --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1abecbc..4b90374 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ # Using multiple databases with the official PostgreSQL Docker image -This fork merges [heliocastro:user_pass_improvements](https://github.com/heliocastro/docker-postgresql-multiple-databases/tree/user_pass_improvement) +This fork merges [heliocastro:user_pass_improvements](https://github.com/heliocastro/docker-postgresql-multiple-databases/tree/user_pass_improvement) with some slight tweaks to keep the user name the same as the database name. The [official recommendation](https://hub.docker.com/_/postgres/) for creating multiple databases is as follows: -*If you would like to do additional initialization in an image derived from -this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under -`/docker-entrypoint-initdb.d` (creating the directory if necessary). After the -entrypoint calls `initdb` to create the default `postgres` user and database, -it will run any `*.sql` files and source any `*.sh` scripts found in that -directory to do further initialization before starting the service.* +> If you would like to do additional initialization in an image derived from +> this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under +> `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the +> entrypoint calls `initdb` to create the default `postgres` user and database, +> it will run any `*.sql` files and source any `*.sh` scripts found in that +> directory to do further initialization before starting the service. This directory contains a script to create multiple databases using that mechanism. @@ -28,7 +28,7 @@ Clone the repository, mount its directory as a volume into volumes: - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: - - POSTGRES_MULTIPLE_DATABASES=db1:user:pwd,db2:user:pwd + - POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2 - POSTGRES_USER=myapp - POSTGRES_PASSWORD= - POSTGRES_DB=db @@ -47,7 +47,7 @@ to the container: myapp-postgresql: image: eu.gcr.io/your-project/postgres-multi-db environment: - - POSTGRES_MULTIPLE_DATABASES=db1,db2 + - POSTGRES_MULTIPLE_DATABASES=db1:pwd1,db2:pwd2 - POSTGRES_USER=myapp - POSTGRES_PASSWORD= - POSTGRES_DB=db @@ -57,4 +57,4 @@ to the container: If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: environment: - - POSTGRES_MULTIPLE_DATABASES="test-db-1:user:pwd","test-db-2:user:pwd" + - POSTGRES_MULTIPLE_DATABASES="test-db-1:pwd1","test-db-2:pwd2" From 91ebbe004c5302626d27e64aecc2182e03a9d57e Mon Sep 17 00:00:00 2001 From: Corbin Bartsch Date: Fri, 24 Jun 2022 15:29:24 -0400 Subject: [PATCH 6/6] Upgrade to PostgreSQL 14.4 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ee9c3d0..c66cbe0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,2 @@ -FROM postgres:9.6 +FROM postgres:14.4 COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/