web-service/docker/postgres/databases.sh

24 lines
687 B
Bash
Raw Normal View History

2023-06-29 17:41:36 +00:00
#!/bin/bash
# https://stackoverflow.com/a/59466352
set -e
set -u
function create_user_and_database() {
local database=$(echo $1)
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -d "$database" <<-EOSQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
EOSQL
}
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Creating databases: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Databases created"
fi