Skip to content

Instantly share code, notes, and snippets.

@theoretick
Last active March 2, 2016 19:03
Show Gist options
  • Save theoretick/b9717cae9fec529bcda5 to your computer and use it in GitHub Desktop.
Save theoretick/b9717cae9fec529bcda5 to your computer and use it in GitHub Desktop.
shell script for upgrading postgres 9.3 to 9.4 with postGIS. RUN AS ROOT
#! /bin/bash
#
# Based on http://www.gab.lc/articles/migration_postgresql_9-3_to_9-4
#
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
cd /tmp
sudo -u postgres pg_dumpall > BkpOldPG.sql
# make an extra backup so a system restart somewhere in here doesn't wipe it out.
cp /tmp/BkpOldPG.sql /home/BkpOldPG.sql
# If no backup
if [[ ! $(find /tmp/BkpOldPG.sql -type f -size +1c 2>/dev/null) ]]; then
echo "Something went wrong and a backup could not be created! Exiting..."
exit 1
fi
apt-get update
# apt-get upgrade
apt-get install postgresql-9.4 postgresql-9.4-postgis-2.1
/etc/init.d/postgresql stop
# backup default config files before replacing them
cd /etc/postgresql/9.4/main/
cp pg_hba.conf pg_hba.conf_default # backup pg_hba.conf
cp postgresql postgresql.conf_default # backup postgresql.conf
# Replace new config files with old ones
cp ../../9.3/main/pg_hba.conf ./ # copy pg_hba.conf from old version
cp ../../9.3/main/postgresql.conf ./ # copy postgresql.conf from old version
# Replaces references to the old version (9.3) in the replaced config files
sed -i 's/9.3/9.4/g' postgresql.conf
# Check the replacement
grep '9.' postgresql.conf
# Output should look like:
# data_directory = '/var/lib/postgresql/9.4/main' # use data in another directory
# hba_file = '/etc/postgresql/9.4/main/pg_hba.conf' # host-based authentication file
# ident_file = '/etc/postgresql/9.4/main/pg_ident.conf' # ident configuration file
# external_pid_file = '/var/run/postgresql/9.4-main.pid' # write an extra PID file
# change the configuration of the old version to keep it from starting
# replaces the last line "auto" by "disabled"
sed -i '7,/auto/{s/auto/disabled/}' /etc/postgresql/9.3/main/start.conf
##########################################
echo
read -n1 -rsp $'Press any key to start postgres 9.4 or Ctrl+C to exit...\n'
# Try to start postgreSQL 9.4
/etc/init.d/postgresql start
# [ ok ] Starting PostgreSQL 9.3 database server:.
# [ ok ] Starting PostgreSQL 9.4 database server: main.
# Restore the backups from the old version
cd /tmp
sudo -u postgres /usr/lib/postgresql/9.4/bin/psql -d postgres -f BkpOldPG.sql
# In case you rebooted the server and lost the content of /tmp, you can use the copy of the backup that we made in /home/BkpOldPG.sql
# Check the new version
sudo -u postgres psql -c "SHOW SERVER_VERSION"
##########################################
echo
read -n1 -rsp $'Press any key if above version looks correct or Ctrl+C to exit...\n'
# Remove the old version
# `dpkg -l | grep postgresql`
# We now just have to remove all the packages relative to 9.3
# The list might depend with your installation.
apt-get remove postgresql-9.3 postgresql-9.3-ip4r postgresql-9.3-postgis-2.1 postgresql-9.3-postgis-scripts postgresql-client-9.3 postgresql-contrib-9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment