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/ diff --git a/README.md b/README.md index b391923..934b2d7 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,21 @@ 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 + postgres: + image: ismail0352/postgres-multi-user-and-db + ports: + - "5432" 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= + # # 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 -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..9331da7 --- /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 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" + + 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