From d68102a58eaa57635a6b8e244176c37219cfab75 Mon Sep 17 00:00:00 2001 From: Ismail Date: Wed, 25 Oct 2023 17:23:42 +0530 Subject: [PATCH 1/4] Add the updated script --- Dockerfile | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1f014f2..169850b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,3 @@ -FROM library/postgres:9.6-alpine +FROM postgres:12.16 -COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ - -RUN chmod +x /docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh - -EXPOSE 5432 +COPY create-multiple-postgresql-databases-and-user.sh /docker-entrypoint-initdb.d/ From 6eba1355a0a24ee7cb62c0f0bdc67e59441152e8 Mon Sep 17 00:00:00 2001 From: Ismail Date: Wed, 25 Oct 2023 17:33:39 +0530 Subject: [PATCH 2/4] Updated ReadME and more expressive user/db creation script --- README.md | 33 ++++--------------- ...-multiple-postgresql-databases-and-user.sh | 29 ++++++++++++++++ create-multiple-postgresql-databases.sh | 23 ------------- 3 files changed, 35 insertions(+), 50 deletions(-) create mode 100755 create-multiple-postgresql-databases-and-user.sh delete mode 100755 create-multiple-postgresql-databases.sh diff --git a/README.md b/README.md index b391923..1f108be 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# Using multiple databases with the official PostgreSQL Docker image +# Using multiple users and databases with the official PostgreSQL Docker image -The [official recommendation](https://hub.docker.com/_/postgres/) for creating -multiple databases is as follows: +The [official recommendation](https://hub.docker.com/_/postgres/): *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 @@ -19,37 +18,17 @@ mechanism. 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 +`POSTGRES_MULTIPLE_USERS_AND_DATABASES` environment variable as follows (`docker-compose` syntax): myapp-postgresql: image: postgres:9.6.2 - volumes: - - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: - - POSTGRES_MULTIPLE_DATABASES="DB1,ownerOfDB1: DB2,ownerOfDB2: ...DB(n), ownerOfDB(n)" - - POSTGRES_PASSWORD= - -### By building a custom image - -Clone the repository, build and push the image to your Docker repository, -for example for Google Private Repository do the following: - - docker build --tag=eu.gcr.io/your-project/postgres-multi-db . - gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db - -You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable -to the container: - - myapp-postgresql: - image: eu.gcr.io/your-project/postgres-multi-db - environment: - - POSTGRES_MULTIPLE_DATABASES="DB1,ownerOfDB1: DB2,ownerOfDB2: ...DB(n), ownerOfDB(n)" - - POSTGRES_PASSWORD= + - POSTGRES_MULTIPLE_USERS_AND_DATABASES="ownerOfDB1,DB1: ownerOfDB2,DB2: ...ownerOfDB(n), DB(n)" ### Non-standard database names -If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: +If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_USERS_AND_DATABASES`: environment: - - POSTGRES_MULTIPLE_DATABASES="DB1","ownerOfDB1": "DB2","ownerOfDB2": ..."DB(n)", "ownerOfDB(n)" + - POSTGRES_MULTIPLE_USERS_AND_DATABASES="ownerOfDB1","DB1": "ownerOfDB2","DB2": ..."ownerOfDB(n), "DB(n)"" diff --git a/create-multiple-postgresql-databases-and-user.sh b/create-multiple-postgresql-databases-and-user.sh new file mode 100755 index 0000000..c06129f --- /dev/null +++ b/create-multiple-postgresql-databases-and-user.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -e +set -u +# set -x + +POSTGRES_USER="${POSTGRES_USER:-postgres}" + +function create_user_and_database() { + local owner=$(echo $1 | tr ',' ' ' | awk '{print $1}') + local database=$(echo $1 | tr ',' ' ' | awk '{print $2}') + echo " Creating user '$owner' and database '$database'" + + psql -U postgres -tc "SELECT 1 FROM pg_user WHERE usename = '$owner'" | grep -q 1 || psql -U postgres -c "CREATE USER $owner;" + + psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = '$database'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE $database" + + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + GRANT ALL PRIVILEGES ON DATABASE $database TO $owner; +EOSQL +} + +if [ -n "$POSTGRES_MULTIPLE_USERS_AND_DATABASES" ]; then + echo "Multiple database/user creation requested: $POSTGRES_MULTIPLE_USERS_AND_DATABASES" + for details in $(echo $POSTGRES_MULTIPLE_USERS_AND_DATABASES | tr ':' ' '); do + create_user_and_database $details + done + echo "Multiple databases created" +fi diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh deleted file mode 100755 index 83f5adb..0000000 --- a/create-multiple-postgresql-databases.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -set -e -set -u - -function create_user_and_database() { - local database=$(echo $1 | tr ',' ' ' | awk '{print $1}') - local owner=$(echo $1 | tr ',' ' ' | awk '{print $2}') - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $owner IF NOT EXISTS; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $owner; -EOSQL -} - -if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then - echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" - for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ':' ' '); do - create_user_and_database $db - done - echo "Multiple databases created" -fi From 7f4bab382532646f1929a538570d69945184d101 Mon Sep 17 00:00:00 2001 From: Ismail Date: Wed, 25 Oct 2023 22:38:14 +0530 Subject: [PATCH 3/4] user created would be SUPERUSER and permission to CREATEDB and CREATEROLE --- create-multiple-postgresql-databases-and-user.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create-multiple-postgresql-databases-and-user.sh b/create-multiple-postgresql-databases-and-user.sh index c06129f..9331da7 100755 --- a/create-multiple-postgresql-databases-and-user.sh +++ b/create-multiple-postgresql-databases-and-user.sh @@ -11,7 +11,7 @@ function create_user_and_database() { local database=$(echo $1 | tr ',' ' ' | awk '{print $2}') echo " Creating user '$owner' and database '$database'" - psql -U postgres -tc "SELECT 1 FROM pg_user WHERE usename = '$owner'" | grep -q 1 || psql -U postgres -c "CREATE USER $owner;" + psql -U postgres -tc "SELECT 1 FROM pg_user WHERE usename = '$owner'" | grep -q 1 || psql -U postgres -c "CREATE USER $owner WITH LOGIN SUPERUSER CREATEDB CREATEROLE;" psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = '$database'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE $database" From 77126b7e0fba3d00424778c05f4cddda1049bba8 Mon Sep 17 00:00:00 2001 From: Ismail Date: Thu, 26 Oct 2023 12:35:56 +0530 Subject: [PATCH 4/4] README example update --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1f108be..934b2d7 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,14 @@ Clone the repository, mount its directory as a volume into `POSTGRES_MULTIPLE_USERS_AND_DATABASES` environment variable as follows (`docker-compose` syntax): - myapp-postgresql: - image: postgres:9.6.2 + postgres: + image: ismail0352/postgres-multi-user-and-db + ports: + - "5432" environment: - - POSTGRES_MULTIPLE_USERS_AND_DATABASES="ownerOfDB1,DB1: ownerOfDB2,DB2: ...ownerOfDB(n), DB(n)" + # # Syntax for multiple users and multiple DB's + # # - POSTGRES_MULTIPLE_USERS_AND_DATABASES="ownerOfDB1,DB1: ownerOfDB2,DB2: ...ownerOfDB(n), DB(n)" + POSTGRES_MULTIPLE_USERS_AND_DATABASES: "my_user,my_DB: your_user,your_DB" ### Non-standard database names