From 2c047bab97d6839ade176a8f25e26b2e96fd4e57 Mon Sep 17 00:00:00 2001 From: Corbin Bartsch Date: Fri, 24 Jun 2022 15:06:41 -0400 Subject: [PATCH] Keep user name same as database name --- create-multiple-postgresql-databases.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index 40c026f..132689e 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -5,14 +5,14 @@ set -u function create_user_and_database() { local dbinfo=$1 - IFS=":" read -r database user password <<< "$dbinfo" + IFS=":" read -r database password <<< "$dbinfo" echo " Creating user and database '$database'" echo "Creating database '$database' with user '$user' and password '$password'" psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "postgres" <<-EOSQL - SELECT 'CREATE USER ' || LOWER(TRIM('$user')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$user')));\gexec - ALTER USER $user WITH ENCRYPTED PASSWORD '$password'; + SELECT 'CREATE USER ' || LOWER(TRIM('$database')) AS create_user_query WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = LOWER(TRIM('$database')));\gexec + ALTER USER $database WITH ENCRYPTED PASSWORD '$password'; SELECT 'CREATE DATABASE ' || LOWER(TRIM('$database')) || ' WITH OWNER "$POSTGRES_USER" ENCODING "UTF8" LC_COLLATE = "en_US.UTF-8" LC_CTYPE = "en_US.UTF-8" TEMPLATE="template0"' AS create_table_query WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = LOWER(TRIM('$database')));\gexec - GRANT ALL PRIVILEGES ON DATABASE $database TO $user; + GRANT ALL PRIVILEGES ON DATABASE $database TO $database; EOSQL }