Upgrade PostgreSQL database from version 11 to 12

Table of contents

No heading

No headings in the article.

Instead of explaining about PostgreSQL, I will directly jump on steps for upgrading PostgreSQL on Ubuntu/Debian. Though I am doing it for version 11 but it should work for other versions also.

Note: First backup complete data using pg_dump or pg_dumpall.

  • First check current version in psql console.
postgres=# SELECT version();
                                                             version                                                             
---------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 11.9 (Ubuntu 11.9-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
(1 row)
  • Stop the services for both the versions (11 and 12).
$ sudo systemctl stop postgresql@11-main.service postgresql@12-main.service
  • Switch to postgres user and switch to its home directory so that it has permission to create any logs if required.
$ sudo su postgres
$ cd ~
  • Run a test if everything is okay.
$ /usr/lib/postgresql/12/bin/pg_upgrade --old-datadir=/var/lib/postgresql/11/main --new-datadir=/var/lib/postgresql/12/main --old-bindir=/usr/lib/postgresql/11/bin --new-bindir=/usr/lib/postgresql/12/bin --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' -O "-c timescaledb.restoring=on" --check

It will print output something like this -

------------------------------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*
  • Start upgrading.
$ /usr/lib/postgresql/12/bin/pg_upgrade --old-datadir=/var/lib/postgresql/11/main --new-datadir=/var/lib/postgresql/12/main --old-bindir=/usr/lib/postgresql/11/bin --new-bindir=/usr/lib/postgresql/12/bin --old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' -O "-c timescaledb.restoring=on"

The output is something like this-

Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting oldest XID for new cluster                          ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

Upgrade Complete lines says our upgrade is completed successfully. In the end, two scripts are shown which can be run further after verifying everything.

  • Verify version:
postgres=# SELECT version();
                                                              version                                                               
------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.10 (Ubuntu 12.10-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit