scripts/postgres-roles.sh
#!/usr/bin/env bash
# Parameters:
# 1 - postgres master DB user
# 2 - postgres master DB password
# 3 - postgres DB host
# 4 - vivaria user password
# 5 - vivariaro user password
#
# This script attempts to set up the roles "vivaria", "vivariaro", "pokereadonly", and "metabase"
# wherever you are running postgres.
# It is designed to be idempotent-ish, but could well create errors depending on the DB state.
# Analyse its output carefully.

set -u

export PGUSER="$1"
export PGPASSWORD="$2"
export PGHOST="$3"
export PGPORT=5432

run_sql_failure_ok() {
    psql --dbname postgres -v ON_ERROR_STOP=1 -c "$1" || echo "$2"
}

psql --dbname postgres -v ON_ERROR_STOP=1 -c "SELECT 1"

run_sql_failure_ok "CREATE ROLE vivaria LOGIN PASSWORD '$4'" 'Error creating vivaria user; hopefully it already exists'
run_sql_failure_ok "CREATE ROLE vivariaro LOGIN PASSWORD '$5'" 'Error creating vivariaro user; hopefully it already exists'

run_sql_failure_ok "CREATE DATABASE vivariadb" 'Error creating vivariadb database; hopefully it already exists'

psql --dbname postgres -v ON_ERROR_STOP=1 <<EOF
GRANT CONNECT ON DATABASE vivariadb TO vivaria;
GRANT ALL PRIVILEGES ON DATABASE "vivariadb" to vivaria;
EOF

# The following roles are not currently used, but must exist for the schema script to run without errors
run_sql_failure_ok "CREATE ROLE pokereadonly" 'Error creating pokereadonly user'
run_sql_failure_ok "CREATE ROLE metabase" 'Error creating metabase user'

psql --dbname vivariadb <<'EOF'
GRANT ALL ON SCHEMA "public" to "vivaria";
GRANT USAGE ON SCHEMA public TO "vivariaro";
EOF

# Make sure when vivaria user creates tables, as part of a migration, that vivariaro can read them
export PGUSER=vivaria
export PGPASSWORD="$4"
psql --dbname vivariadb <<EOF
GRANT SELECT ON ALL TABLES IN SCHEMA public TO "vivariaro";
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO "vivariaro";
EOF
