Skip to content

Add example to many users #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
57 changes: 57 additions & 0 deletions create-multiple-postgresql-full.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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