Upgrade PostgreSQL from 9.5 to 9.6 on Ubuntu 17.04

Howto guide for upgrading PostgreSQL from version 9.5 to 9.6 on Ubuntu, after its upgrade from version 16.10 to 17.04 (Zesty Zapus).

© 2017 Paolo Melchiorre CC BY-SA “Photo taken at sunset on the Arno river in the center of Florence (Italy) during the PyCon Italia 2017”
© 2017 Paolo Melchiorre CC BY-SA “Photo taken at sunset on the Arno river in the center of Florence (Italy) during the PyCon Italia 2017”
Upgrade PostgreSQL on Ubuntu (7 part series)
  1. Upgrade PostgreSQL from 9.5 to 9.6 on Ubuntu 17.04
  2. Upgrade PostgreSQL from 9.6 to 10 on Ubuntu 18.04
  3. Upgrade PostgreSQL from 10 to 11 on Ubuntu 19.04
  4. Upgrade PostgreSQL from 11 to 12 on Ubuntu 20.04
  5. Upgrade PostgreSQL from 12 to 13 on Ubuntu 21.04
  6. Upgrade PostgreSQL from 13 to 14 on Ubuntu 22.04
  7. Upgrade PostgreSQL from 14 to 15 on Ubuntu 23.04

TL;DR

After upgrade Ubuntu from version 16.10 to 17.04:

$ sudo pg_dropcluster 9.6 main --stop
$ sudo pg_upgradecluster 9.5 main
$ sudo pg_dropcluster 9.5 main

Goal

This article is aimed at those like me who use Ubuntu and PostgreSQL to develop locally on their computer and after the last update to Ubuntu 17.04 they have two versions of PostgreSQL installed.

Upgrade PostgreSQL

During Ubuntu updgrade to 17.04 you receive this message “Configuring postgresql-common”:

Obsolete major version 9.5

The PostgreSQL version 9.5 is obsolete, but the server or client packages are still installed.

Please install the latest packages (postgresql-9.6 and postgresql-client-9.6) and upgrade the existing clusters with pg_upgradecluster (see manpage).

Please be aware that the installation of postgresql-9.6 will automatically create a default cluster 9.6/main.

If you want to upgrade the 9.5/main cluster, you need to remove the already existing 9.6 cluster (pg_dropcluster --stop 9.6 main, see manpage for details).

The old server and client packages are no longer supported.

After the existing clusters are upgraded, the postgresql-9.5 and postgresql-client-9.5 packages should be removed.

Please see /usr/share/doc/postgresql-common/README.Debian.gz for details.

Use dpkg -l | grep postgresql to check which versions of postgres are installed:

ii postgresql               9.6+179              all   …
ii postgresql-9.5           9.5.6-0ubuntu0.16.10 amd64 …
ii postgresql-9.6           9.6.2-1              amd64 …
ii postgresql-client        9.6+179              all   …
ii postgresql-client-9.5    9.5.6-0ubuntu0.16.10 amd64 …
ii postgresql-client-9.6    9.6.2-1              amd64 …
ii postgresql-client-common 179                  all   …
ii postgresql-common        179                  all   …
ii postgresql-contrib-9.5   9.5.6-0ubuntu0.16.10 amd64 …
ii postgresql-contrib-9.6   9.6.2-1              amd64 …

Run pg_lsclusters, your 9.5 and 9.6 main clusters should be “online”.

Ver Cluster Port Status Owner    Data directory               Log file
9.5 main    5433 online postgres /var/lib/postgresql/9.5/main …
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main …

There already is a cluster “main” for 9.6 (since this is created by default on package installation). This is done so that a fresh installation works out of the box without the need to create a cluster first, but of course it clashes when you try to upgrade 9.5/main when 9.6/main also exists. The recommended procedure is to remove the 9.6 cluster with pg_dropcluster and then upgrade with pg_upgradecluster.

Stop the 9.6 cluster and drop it.

$ sudo pg_dropcluster 9.6 main --stop

Upgrade the 9.5 cluster to the latest version.

$ sudo pg_upgradecluster 9.5 main

Your 9.5 cluster should now be “down” and you can verifity running pg_lsclusters

Ver Cluster Port Status Owner    Data directory               Log file
9.5 main    5433 down   postgres /var/lib/postgresql/9.5/main …
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main …

Check that the upgraded cluster works, then remove the 9.5 cluster.

$ sudo pg_dropcluster 9.5 main

After all your data check you can remove your old packages.

$ sudo apt purge \
postgresql-9.5 \
postgresql-client-9.5 \
postgresql-contrib-9.5

Disclaimer

There is no warranty for the program, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the program “as is” without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.