storage/init/postgres-init.sh
Sergei Poljanski 459ee15f00
All checks were successful
Deploy storage stack / deploy (push) Successful in 14s
postgres-init: enable citext extension on asxpio db
Needed for case-insensitive email columns in the asxpio users table.
Runs as the Postgres superuser inside the init container; idempotent.
2026-05-26 19:07:16 +03:00

36 lines
926 B
Bash

#!/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"