Well i do "pkg upgrade". And i end up with versions too new, where i should have used to older versions to make dumps and to reimport.
My procedure is "broken"....
Feel free to advice.
My procedure is "broken"....
Feel free to advice.
oot@myfreebsd:/var/db/postgres # cat upgrade_postgresql
#!/bin/sh
set -eu
OLD_VER="18"
NEW_VER="19"
PG_HOME="/var/db/postgres"
DUMP_FILE="${PG_HOME}/pg${OLD_VER}_full_backup.sql"
OLD_DATA="${PG_HOME}/data${OLD_VER}"
NEW_DATA="${PG_HOME}/data${NEW_VER}"
# Ensure script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "Error: This script must be run as root." >&2
exit 1
fi
echo "==> Step 1: Dumping PostgreSQL ${OLD_VER} databases and roles..."
su -l postgres -c "pg_dumpall -f '${DUMP_FILE}'"
# Verify dump file exists and is non-empty
if [ ! -s "${DUMP_FILE}" ]; then
echo "Error: Dump file ${DUMP_FILE} is missing or empty. Aborting!" >&2
exit 1
fi
echo "Dump successful: $(ls -lh "${DUMP_FILE}" | awk '{print $5}')"
echo "==> Step 2: Backing up configuration files..."
cp -p "${OLD_DATA}/postgresql.conf" "${PG_HOME}/postgresql.conf.pg${OLD_VER}.bak"
cp -p "${OLD_DATA}/pg_hba.conf" "${PG_HOME}/pg_hba.conf.pg${OLD_VER}.bak"
echo "==> Step 3: Stopping PostgreSQL ${OLD_VER} and swapping packages..."
service postgresql stop
pkg delete -y "postgresql${OLD_VER}-server" "postgresql${OLD_VER}-client"
pkg install -y "postgresql${NEW_VER}-server" "postgresql${NEW_VER}-client"
echo "==> Step 4: Initializing PostgreSQL ${NEW_VER}..."
service postgresql initdb
echo ""
echo "========================================================================="
echo "PAUSE: PostgreSQL ${NEW_VER} is initialized at ${NEW_DATA}."
echo "If you had custom settings, review and merge them now from:"
echo " - ${PG_HOME}/postgresql.conf.pg${OLD_VER}.bak"
echo " - ${PG_HOME}/pg_hba.conf.pg${OLD_VER}.bak"
echo "Press [Enter] when ready to start PostgreSQL ${NEW_VER} and restore data..."
echo "========================================================================="
read -r _
echo "==> Step 5: Starting PostgreSQL ${NEW_VER} and importing dump..."
service postgresql start
su -l postgres -c "psql -v ON_ERROR_STOP=1 -f '${DUMP_FILE}' postgres"
echo "==> Step 6: Rebuilding optimizer statistics..."
su -l postgres -c "vacuumdb --all --analyze-in-stages"
echo ""
echo "Upgrade to PostgreSQL ${NEW_VER} complete!"
echo "Once verified, remember to clean up:"
echo " rm -rf '${OLD_DATA}' '${DUMP_FILE}'"
root@myfreebsd:/var/db/postgres #