#!/bin/sh # Idempotent bootstrap: create per-app roles and databases. # Re-running is safe — checks before creating, updates passwords if changed. set -eu ensure_role() { role="$1" password="$2" exists=$(psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${role}'") if [ "${exists}" = "1" ]; then psql -c "ALTER ROLE \"${role}\" WITH LOGIN PASSWORD '${password}';" else psql -c "CREATE ROLE \"${role}\" WITH LOGIN PASSWORD '${password}';" fi } ensure_db() { db="$1" owner="$2" exists=$(psql -tAc "SELECT 1 FROM pg_database WHERE datname='${db}'") if [ "${exists}" != "1" ]; then psql -c "CREATE DATABASE \"${db}\" OWNER \"${owner}\";" fi } ensure_extension() { db="$1" ext="$2" psql -d "${db}" -c "CREATE EXTENSION IF NOT EXISTS \"${ext}\";" } ensure_role "asxpio" "${ASXPIO_DB_PASSWORD}" ensure_db "asxpio" "asxpio" ensure_extension "asxpio" "citext" echo "postgres-init: done"