Upgrading postgres

Make sure to have backups, this is mostly a reference for myself :)

# Stop postgres

# Make sure both the previously running and new version of postgres is
# installed

# Create a temporary upgrade directory, eg. /var/lib/postgresql/_tmp and make
# it owned by for postgres:postgres

# Set default version
pg_versions set-default <new version>

# Make sure that the old cluster will not be written to if the wrong postgres
# version starts again
# You have actual backups, right? :)
mv /var/lib/postgresql/<old version>/data \
    /var/lib/postgresql/<old version>/data_

# Create new data directory
mkdir /var/lib/postgresql/<new version>/data
chown postgres:postgres /var/lib/postgresql/<new version>/data

# Go to the temporary directory, init new db, migrate data

# Use the same checksum setting as the original db has been initialized with
# and make sure the locales are installed :)
initdb -D /var/lib/postgresql/<new version>/data \
    --locale=en_US.UTF-8 \
    --encoding=UTF8 \
    --data-checksums

pg_upgrade -b /usr/libexec/postgresql<old version> \
    -B /usr/libexec/postgresql<new version> \
    -d /var/lib/postgresql/<old version>/data_ \
    -D /var/lib/postgresql/<new version>/data

# Automatically generated by pg_upgrade on success, removes the old data dir
./delete_old_cluster.sh

# Remove the old, now empty, directory
rm -r /var/lib/postgresql/<old version>

# Start postgres again
^ back to top