From 60a7893392366cb0be18ddb3d19f3a224b7e948a Mon Sep 17 00:00:00 2001 From: Amnisia Date: Wed, 31 Jan 2018 12:07:27 +0500 Subject: [PATCH] add example to many users --- README.md | 4 +++ create-multiple-postgresql-full.sh | 57 ++++++++++++++++++++++++++++++ docker-compose.yml | 17 +++++++++ 3 files changed, 78 insertions(+) create mode 100644 create-multiple-postgresql-full.sh create mode 100644 docker-compose.yml diff --git a/README.md b/README.md index 3db6ca3..5e7691d 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,7 @@ If you need to use non-standard database names (hyphens, uppercase letters etc), environment: - POSTGRES_MULTIPLE_DATABASES="test-db-1","test-db-2" + + +The `create-multiple-postgresql-full.sh` and `docker-compose.yml` shows an example of how to create both databases and users exclusively to them and assign passwords to them. +`user1` will not have access to `db1`,`db2`. diff --git a/create-multiple-postgresql-full.sh b/create-multiple-postgresql-full.sh new file mode 100644 index 0000000..da1e371 --- /dev/null +++ b/create-multiple-postgresql-full.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +set -e +set -u + +containsElement () { + IFS=$'\n' + local array=$1 val=$2 result=0 + for var in $array; do + if [[ "$var" == "$val" ]]; then + result=1 + break + fi + done + echo "$result" +} + +function create_user_and_database() { + local database=$1 user=$2 password=$3 + local query_users="select rolname from pg_roles;" query_databases="select datname from pg_database;" + local roles=$(echo "$query_users" | psql -tA) + local databases=$(echo "$query_databases" | psql -Aqt) + user_exist=$(containsElement "$roles" "$user") + database_exist=$(containsElement "$databases" "$database") + + if [[ "$user_exist" == 1 ]]; then + echo "User '$user' exist. Skipping" + else + echo "Create User '$user'."; + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" \ + -c "CREATE USER $user WITH PASSWORD '$password'"; + fi + if [[ "$database_exist" == 1 ]]; then + echo "Database '$database' exists. Skipping." + else + echo "Create Database '$database'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" \ + -c "CREATE DATABASE $database;" + fi + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" \ + -c "GRANT ALL PRIVILEGES ON DATABASE $database TO $user;" +} + +if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then + echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" + IFS=', ' read -r -a databases <<< "$POSTGRES_MULTIPLE_DATABASES" + IFS=', ' read -r -a users <<< "$POSTGRES_MULTIPLE_USERS" + IFS=', ' read -r -a passwords <<< "$POSTGRES_MULTIPLE_PASSWORDS" + for index in ${!databases[@]}; do + if [[ $index < ${#databases[*]} && $index < ${#users[*]} && $index < ${#passwords[*]} ]] ; then + create_user_and_database "${databases[index]//[\'\"\`]/}" "${users[index]//[\'\"\`]/}" "${passwords[index]//[\'\"\`]/}" + else + echo "DATABASE '${databases[index]}' OR USER '${users[index]}' EMPTY" + fi + done + echo "Multiple databases created" +fi diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..21dee4c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: "2" + +services: + postgres: + image: postgres:alpine + container_name: example-postgres + environment: + - POSTGRES_MULTIPLE_DATABASES="db1","db2","db3" + - POSTGRES_MULTIPLE_USERS="user1","user2","user3 + - POSTGRES_MULTIPLE_PASSWORDS="password1","password2","password3" + - POSTGRES_USER=superrole + - POSTGRES_PASSWORD=superpassword + ports: + - "5432:5432" + volumes: + - ./create-multiple-postgresql-full.sh:/docker-entrypoint-initdb.d/create-multiple-postgresql-full.sh + restart: always