From d22e6f146c071e2c24e44f3294e5046e69769ad1 Mon Sep 17 00:00:00 2001 From: arcbjorn Date: Mon, 24 Oct 2022 00:59:32 -0300 Subject: [PATCH] feat(pgSQL): add multiple DBs & users script --- .../create-multiple-postgresql-databases.sh | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 postgresql/create-multiple-postgresql-databases.sh diff --git a/postgresql/create-multiple-postgresql-databases.sh b/postgresql/create-multiple-postgresql-databases.sh new file mode 100644 index 0000000..400ea42 --- /dev/null +++ b/postgresql/create-multiple-postgresql-databases.sh @@ -0,0 +1,24 @@ +#!/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; + ALTER USER $owner PASSWORD $database; + 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 \ No newline at end of file