Files
configpolicy/roles/postgresql-server-base/files/postgresql-upgrade.sh
Dustin C. Hatch e861883627 r/pgsql-server-base: Add post-upgrade capability
The `postgresql-upgrade` script will now run any executables located in
the `/etc/postgresql/post-upgrade.d` directory.  This will allow making
arbitrary changes to the system after a PostgreSQL major version
upgrade.  Notably, we will use this capability to change the WAL-G
configuration to upload WAL archives and backups to the correct
version-specific location.
2024-11-17 10:27:31 -06:00

51 lines
1.4 KiB
Bash

#!/bin/sh
# vim: set sw=4 ts=4 sts=4 et :
set -e
CONFIG_DIR=/etc/postgresql
data_directory=$(
runuser -u postgres -- postgres -D "${CONFIG_DIR}" -C data_directory
)
old_version=$(cat "${data_directory}"/PG_VERSION)
current_version=$(postgres --version | sed -r 's/.*\s+([0-9]+).*/\1/')
if [ "${old_version}" = "${current_version}" ]; then
exit 0
fi
config_dir_old="${CONFIG_DIR}.${old_version}"
rm -rf "${config_dir_old}"
cp -a "${CONFIG_DIR}" "${config_dir_old}"
sed -ri.orig \
-e 's/data_directory\s*=\s*'\''(.*?)'\''/data_directory = '\''\1.'"${old_version}"\''/' \
-e 's/^(\s*ssl\s*=\s*)(.*)$/\1off/' \
"${config_dir_old}"/postgresql.conf
sed -i.orig \
-e '$alocal all postgres peer' \
-e d \
"${config_dir_old}"/pg_hba.conf
mv "${data_directory}" "${data_directory}.${old_version}"
runuser -u postgres -- initdb -A peer --no-instructions "${data_directory}"
runuser -u postgres -- pg_upgrade \
-b "/usr/lib64/pgsql/postgresql-${old_version}"/bin \
-B /usr/bin \
-d "${config_dir_old}" \
-D "${CONFIG_DIR}" \
-o '-c restore_command= -c archive_command=' \
-O '-c restore_command= -c archive_command=' \
-k
for f in /etc/postgresql/post-upgrade.d/*; do
if [ -x "${f}" ]; then
printf 'Running post-update script: %s\n' "${f}" >&2
"${f}" || {
printf 'Post-update script %s failed\n' "${f}" >&2
continue
}
fi
done