#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    -- object: create_user_postgres | type: Generic SQL Object --
    DROP ROLE IF EXISTS $POSTGRES_USER_PROFILE;
    CREATE ROLE $POSTGRES_USER_PROFILE SUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT LOGIN PASSWORD '$POSTGRES_USER_PROFILE_PASSWORD';

    DROP ROLE IF EXISTS $POSTGRES_USER_API_AUTH;
    CREATE ROLE $POSTGRES_USER_API_AUTH WITH LOGIN ENCRYPTED PASSWORD '$POSTGRES_USER_API_AUTH_PASSWORD';

    -- object: $POSTGRES_USER_API_ANON | type: ROLE --
    DROP ROLE IF EXISTS $POSTGRES_USER_API_ANON;
    CREATE ROLE $POSTGRES_USER_API_ANON WITH ROLE $POSTGRES_USER_API_AUTH;
EOSQL
