#!/bin/bash # Co-op Cloud wrapper around pgautoupgrade's entrypoint. # # pgautoupgrade runs pg_upgrade assuming the old cluster's bootstrap superuser # (the "install user", oid 10) equals $POSTGRES_USER. That is not always true # (e.g. a cluster bootstrapped with the default "postgres" superuser and a # separate "discourse" app role), and a mismatch makes pg_upgrade's consistency # check fail (pgautoupgrade issue #115). When a major-version upgrade is pending, # detect the old cluster's real install user and run the upgrade as that user; # pgautoupgrade handles everything else (old binaries, initdb, pg_upgrade). set -e if [ -s "$PGDATA/PG_VERSION" ]; then OLD_VERSION="$(cat "$PGDATA/PG_VERSION")" TARGET_VERSION="${PGTARGET%%.*}" if [ -n "$OLD_VERSION" ] && [ -n "$TARGET_VERSION" ] && [ "$OLD_VERSION" != "$TARGET_VERSION" ]; then # query the old cluster in single-user mode (no socket/auth needed) with # pgautoupgrade's bundled old-version binaries OLD_POSTGRES="/usr/local-pg${OLD_VERSION}/bin/postgres" if [ -x "$OLD_POSTGRES" ]; then INSTALL_USER="$(echo 'select rolname from pg_authid where oid = 10;' \ | gosu postgres "$OLD_POSTGRES" --single -D "$PGDATA" template1 2>/dev/null \ | sed -n 's/.*rolname = "\([^"]*\)".*/\1/p' | head -n1)" if [ -n "$INSTALL_USER" ]; then echo "cc-db-entrypoint: old cluster (pg$OLD_VERSION) install user is '$INSTALL_USER'; running the pg$TARGET_VERSION upgrade as that user" export POSTGRES_USER="$INSTALL_USER" else echo "cc-db-entrypoint: WARNING could not detect old install user; letting pgautoupgrade use POSTGRES_USER=$POSTGRES_USER" fi fi fi fi exec /usr/local/bin/docker-entrypoint.sh "$@"