docker-postgresql-multiple-.../create-multiple-postgresql-databases.sh
Helio Chissini de Castro 6151b43cba
Simple script improvement to add the capability of different user and password per database entry
Signed-off-by: Helio Chissini de Castro <helio@kde.org>
2021-12-15 21:29:57 +01:00

26 lines
741 B
Bash
Executable File

#!/bin/bash
set -e
set -u
function create_user_and_database() {
local dbinfo=$1
IFS=":" read -r database user password <<< "$dbinfo"
echo "Creating database '$database' with user '$user' and password '$password'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $user;
ALTER USER $user WITH ENCRYPTED PASSWORD '$password';
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $user;
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