This commit is contained in:
commit
479e3d5cf9
10 changed files with 466 additions and 0 deletions
29
init/postgres-init.sh
Normal file
29
init/postgres-init.sh
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/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_role "asxpio" "${ASXPIO_DB_PASSWORD}"
|
||||
ensure_db "asxpio" "asxpio"
|
||||
|
||||
echo "postgres-init: done"
|
||||
Loading…
Add table
Add a link
Reference in a new issue