coreapp new --postgres: apps with their own database

A template overlay (templates/postgres) adds DEPENDS="postgresql", DB_*
settings with a random password, an install that creates the user and
database on the Core's PostgreSQL (safe to re-run), an uninstall that
drops them, and a Python example that counts visits. Tested with a
throwaway app: deploy, redeploy and a reboot of the Core keep the data,
remove drops the database and user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 12:06:55 -07:00
co-authored by Claude Opus 5.5
parent 1108060432
commit 74bdc13f0f
12 changed files with 211 additions and 9 deletions
+18
View File
@@ -0,0 +1,18 @@
. /federated/lib/helpers.sh
installGeneric {{NAME}}
# .env holds secrets
chmod 600 /federated/apps/{{NAME}}/.env
# Its own database and user on the Core's PostgreSQL (DEPENDS=postgresql),
# with the password from .env. Safe to run again: an existing user gets the
# password, an existing database is kept.
DB_PASSWORD="$(sed -n 's/^DB_PASSWORD=//p' /federated/apps/{{NAME}}/.env)"
if docker exec postgresql psql -U postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname = '{{DBNAME}}'" | grep -q 1; then
docker exec postgresql psql -U postgres -qc "ALTER USER {{DBNAME}} WITH PASSWORD '$DB_PASSWORD'"
else
docker exec postgresql psql -U postgres -qc "CREATE USER {{DBNAME}} WITH PASSWORD '$DB_PASSWORD'"
fi
if ! docker exec postgresql psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '{{DBNAME}}'" | grep -q 1; then
docker exec postgresql psql -U postgres -qc "CREATE DATABASE {{DBNAME}} OWNER {{DBNAME}}"
fi
unset DB_PASSWORD