Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mdesantis/ebab306c0bf13f785b93e7894ca0cde2 to your computer and use it in GitHub Desktop.
Save mdesantis/ebab306c0bf13f785b93e7894ca0cde2 to your computer and use it in GitHub Desktop.
Upgrading PostgreSQL from 9.6 to 10 on Ubuntu 16.04

TL;DR

Install Postgres 10, and then:

sudo pg_dropcluster 10 main --stop
sudo pg_upgradecluster 9.6 main
sudo pg_dropcluster 9.6 main

Install PostgreSQL:

sudo echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
  sudo apt-key add -
  
sudo aptitude update

sudo aptitude install postgresql-10 postgresql-client-10 postgresql-contrib-10

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

i   postgresql                                                        - object-relational SQL database (supported version)
i A postgresql-9.6                                                    - object-relational SQL database, version 9.6 server
i A postgresql-10                                                    - object-relational SQL database, version 10 server
i A postgresql-client-9.6                                             - front-end programs for PostgreSQL 9.6
i A postgresql-client-10                                             - front-end programs for PostgreSQL 10
i A postgresql-contrib-9.6                                            - additional facilities for PostgreSQL
i A postgresql-contrib-10                                            - additional facilities for PostgreSQL

Run pg_lsclusters, your 9.6 and 10 main clusters should be "online".

pg_lsclusters 
Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
10  main    5433 online postgres /var/lib/postgresql/10/main  /var/log/postgresql/postgresql-10-main.log

There already is a cluster "main" for 10 (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.6/main when 10/main also exists. The recommended procedure is to remove the 10 cluster with pg_dropcluster and then upgrade with pg_upgradecluster.

Stop the 10 cluster and drop it.

sudo pg_dropcluster 10 main --stop

Upgrade the 9.6 cluster to the latest version.

sudo pg_upgradecluster 9.6 main

Your 9.6 cluster should now be "down".

pg_lsclusters 
Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5433 down   postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
10  main    5432 online postgres /var/lib/postgresql/10/main  /var/log/postgresql/postgresql-10-main.log

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

sudo pg_dropcluster 9.6 main

Lastly:

sudo nano /etc/postgresql/10/main/postgresql.conf

Change cluster_name = '9.6/main' to cluster_name = '10/main'.

@lnxslck
Copy link

lnxslck commented Feb 12, 2019

Great article, but why not use -m upgrade?

sudo pg_upgradecluster -m upgrade 9.6 main

Wouldnt using the upgrade method be better than pg_dump and pg_restore?

@apollotonkosmo
Copy link

Great article, but why not use -m upgrade?

sudo pg_upgradecluster -m upgrade 9.6 main

Wouldnt using the upgrade method be better than pg_dump and pg_restore?

@lnxslck I did use that approach as well with no issues whatsoever

@mdesantis
Copy link
Author

It could happen to run into issues using pg_upgrade. If my mind works correctly it happened to me that pg_upgrade didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment