feat(db): switch to discourse/postgres image with install-user + checksum adapter
All checks were successful
cc-ci/testme cc-ci: success
All checks were successful
cc-ci/testme cc-ci: success
Replace the bitnami-era pgvector:pg17 db + hand-rolled pg_upgrade entrypoint with discourse/postgres:pg18 (pgvector + discourse's auto-upgrade layer, as suggested on coop-cloud/discourse#16). The image does the heavy lifting (installs old binaries, runs pg_upgrade into the versioned PGDATA); a thin cc-db-entrypoint.sh wrapper fills the two gaps it leaves: - secrets: inject DB_PASSWORD/POSTGRES_PASSWORD from the docker secret (the image reads them from env, no *_FILE support); - install user: detect the old cluster's bootstrap superuser (oid 10) and export POSTGRES_USER so pg_upgrade + the new cluster's initdb match it. Real deployments differ (bitnami-origin clusters install as 'postgres' + a 'discourse' app role; others as 'discourse'). The image hardcodes --username=$POSTGRES_USER and never detects this, so the adapter is required; - checksums: pg18's initdb enables data checksums by default but pg13-17 clusters here have them off, and pg_upgrade requires a match -> initdb the new cluster with --no-data-checksums unless the old one reports them on. Other changes: - mount postgresql_data at /var/lib/postgresql (versioned PGDATA .../18/docker) - pg_backup.sh: detect the superuser at runtime; fix paths for the new layout - bump DB_ENTRYPOINT_VERSION v6, PG_BACKUP_VERSION v3 (immutable swarm configs) - drop entrypoint.postgres.sh.tmpl Verified on cctest: upgrade from an existing pg17 cluster (install user 'postgres') -> pg18, all data preserved, serves over HTTPS via Traefik.
This commit is contained in:
41
pg_backup.sh
41
pg_backup.sh
@ -1,44 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Postgres backup/restore hook for the discourse `db` service.
|
||||
# Postgres backup/restore hook for the discourse `db` service (discourse/postgres image).
|
||||
|
||||
set -e
|
||||
|
||||
BACKUP_FILE='/var/lib/postgresql/data/backup.sql'
|
||||
export PGPASSWORD=$(cat "${POSTGRES_PASSWORD_FILE:-/run/secrets/db_password}")
|
||||
DB_USER="${POSTGRES_USER:-discourse}"
|
||||
# discourse/postgres keeps the live cluster at a versioned PGDATA under the
|
||||
# /var/lib/postgresql mount. Write the dump at the volume root so backupbot's
|
||||
# `postgresql_data.path: backup.sql` label captures it.
|
||||
BACKUP_FILE='/var/lib/postgresql/backup.sql'
|
||||
DATADIR="${PGDATA:-/var/lib/postgresql/18/docker}"
|
||||
DB_NAME="${POSTGRES_DB:-discourse}"
|
||||
|
||||
# The bootstrap superuser (install user, oid 10) differs between deployments
|
||||
# (`postgres` on bitnami-origin clusters, `discourse` on others). Detect it at
|
||||
# runtime over the local trust socket rather than hard-coding a name.
|
||||
detect_superuser() {
|
||||
local u name
|
||||
for u in discourse postgres; do
|
||||
name="$(psql -U "$u" -d "$DB_NAME" -tAc 'select rolname from pg_roles where oid = 10' 2>/dev/null | tr -d '[:space:]')"
|
||||
if [ -n "$name" ]; then echo "$name"; return 0; fi
|
||||
done
|
||||
echo postgres
|
||||
}
|
||||
SU="$(detect_superuser)"
|
||||
|
||||
function backup {
|
||||
pg_dump -U "$DB_USER" "$DB_NAME" | gzip > "$BACKUP_FILE"
|
||||
pg_dump -U "$SU" "$DB_NAME" | gzip > "$BACKUP_FILE"
|
||||
}
|
||||
|
||||
function restore {
|
||||
cd /var/lib/postgresql/data/
|
||||
cd "$DATADIR"
|
||||
|
||||
# Block all non-local connections so the running discourse app + sidekiq cannot reconnect and
|
||||
# interfere with the drop/recreate/reimport. Restored on exit.
|
||||
restore_hba() {
|
||||
cat pg_hba.conf.bak > pg_hba.conf
|
||||
rm -f pg_hba.conf.bak
|
||||
su postgres -c 'pg_ctl reload'
|
||||
su postgres -c "pg_ctl -D '$DATADIR' reload"
|
||||
}
|
||||
cp pg_hba.conf pg_hba.conf.bak
|
||||
echo 'local all all trust' > pg_hba.conf
|
||||
su postgres -c 'pg_ctl reload'
|
||||
su postgres -c "pg_ctl -D '$DATADIR' reload"
|
||||
trap restore_hba EXIT INT TERM
|
||||
|
||||
# terminate any lingering local sessions before recreate
|
||||
# see https://stackoverflow.com/questions/5108876/kill-a-postgresql-session-connection
|
||||
psql -U "$DB_USER" -d postgres -c \
|
||||
psql -U "$SU" -d postgres -c \
|
||||
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='${DB_NAME}' AND pid<>pg_backend_pid();"
|
||||
|
||||
# drop database and then recreate it
|
||||
psql -U "$DB_USER" -d postgres -c "DROP DATABASE ${DB_NAME} WITH (FORCE);"
|
||||
createdb -U "$DB_USER" "$DB_NAME"
|
||||
psql -U "$SU" -d postgres -c "DROP DATABASE ${DB_NAME} WITH (FORCE);"
|
||||
createdb -U "$SU" "$DB_NAME"
|
||||
|
||||
# reimport data
|
||||
gunzip -c "$BACKUP_FILE" | psql -U "$DB_USER" -d "$DB_NAME" -1 -v ON_ERROR_STOP=1 -f -
|
||||
# reimport data
|
||||
gunzip -c "$BACKUP_FILE" | psql -U "$SU" -d "$DB_NAME" -1 -v ON_ERROR_STOP=1 -f -
|
||||
}
|
||||
|
||||
$@
|
||||
|
||||
Reference in New Issue
Block a user