18.6. Upgrading a Postgres Pro Cluster

This section discusses how to upgrade your database data from one Postgres Pro release to a newer one.

Current Postgres Pro version numbers consist of three digit groups: two of them are based on the PostgreSQL version, and the third one is the Postgres Pro release number. For example, in the version number 12.11.2, the 12 is the major version number, the 11 is the minor version number, and the 2 is the Postgres Pro release number, meaning this would be the second release after the PostgreSQL release 12.11. For releases before Postgres Pro version 10.0, version numbers consist of four groups, for example, 9.5.3.2. In those cases, the Postgres Pro version consists of the three digit groups of the PostgreSQL version number, e.g., 9.5.3, and the fourth number is the Postgres Pro release number, e.g., 2, meaning this would be the second Postgres Pro release after the PostgreSQL release 9.5.3.

Postgres Pro releases of the same minor version are usually compatible with earlier and later releases. If you are upgrading from Postgres Pro of the same minor version, for example, from 12.11.1 to 12.11.2, it is usually enough to install the new version into your current installation directory. If you are upgrading from Postgres Pro based on another PostgreSQL minor version, for example, from 12.11.1 to 12.15.1, you should always pay attention to the section labeled "Migration" in the release notes (Appendix E). Though you can upgrade from one release version to another without upgrading to intervening versions, you should read the release notes of all intervening versions too.

For major releases of Postgres Pro, the internal data storage format is subject to change, thus complicating upgrades. The traditional method for moving data to a new major version is to dump and restore the database, though this can be slow. A faster method is pg_upgrade. Replication methods are also available, as discussed below.

New major versions also typically introduce some user-visible incompatibilities, so application programming changes might be required. All user-visible changes are listed in the release notes (Appendix E), including all the necessary instructions for migration in the corresponding section.

Cautious users will want to test their client applications on the new version before switching over fully; therefore, it's often a good idea to set up concurrent installations of old and new versions. When testing a Postgres Pro major upgrade, consider the following categories of possible changes:

Administration

The capabilities available for administrators to monitor and control the server often change and improve in each major release.

SQL

Typically this includes new SQL command capabilities and not changes in behavior, unless specifically mentioned in the release notes.

Library API

Typically libraries like libpq only add new functionality, again unless mentioned in the release notes.

System Catalogs

System catalog changes usually only affect database management tools.

Server C-language API

This involves changes in the backend function API, which is written in the C programming language. Such changes affect code that references backend functions deep inside the server.

18.6.1. Upgrading Data via pg_dumpall

One upgrade method is to dump data from one major version of Postgres Pro and restore it in another — to do this, you must use a logical backup tool like pg_dumpall; file system level backup methods will not work. (There are checks in place that prevent you from using a data directory with an incompatible version of Postgres Pro, so no great harm can be done by trying to start the wrong server version on a data directory.)

It is recommended that you use the pg_dump and pg_dumpall programs from the newer version of Postgres Pro, to take advantage of enhancements that might have been made in these programs. Current releases of the dump programs can read data from any server version back to 8.0.

These instructions assume that your existing installation is under the /usr/local/pgsql directory, and that the data area is in /usr/local/pgsql/data. Substitute your paths appropriately.

  1. If making a backup, make sure that your database is not being updated. This does not affect the integrity of the backup, but the changed data would of course not be included. If necessary, edit the permissions in the file /usr/local/pgsql/data/pg_hba.conf (or equivalent) to disallow access from everyone except you. See Chapter 20 for additional information on access control.

    To back up your database installation, type:

    pg_dumpall > outputfile
    

    To make the backup, you can use the pg_dumpall command from the version you are currently running; see Section 25.1.2 for more details. For best results, however, try to use the pg_dumpall command from Postgres Pro Enterprise 14.15.1, since this version contains bug fixes and improvements over older versions. While this advice might seem idiosyncratic since you haven't installed the new version yet, it is advisable to follow it if you plan to install the new version in parallel with the old version. In that case you can complete the installation normally and transfer the data later. This will also decrease the downtime.

  2. Shut down the old server:

    pg_ctl stop
    

    On systems that have Postgres Pro started at boot time, there is probably a start-up file that will accomplish the same thing. For example, on a Red Hat Linux system one might find that this works:

    /etc/rc.d/init.d/postgresql stop
    

    See Chapter 18 for details about starting and stopping the server.

  3. If restoring from backup, rename or delete the old installation directory if it is not version-specific. It is a good idea to rename the directory, rather than delete it, in case you have trouble and need to revert to it. Keep in mind the directory might consume significant disk space. To rename the directory, use a command like this:

    mv /usr/local/pgsql /usr/local/pgsql.old
    

    (Be sure to move the directory as a single unit so relative paths remain unchanged.)

  4. Install the new version of Postgres Pro Enterprise.

  5. Create a new database cluster if needed. Remember that you must execute these commands while logged in to the special database user account (which you already have if you are upgrading).

    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
    

  6. Restore your previous pg_hba.conf and any postgresql.conf modifications.

  7. Start the database server, again using the special database user account:

    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
    

  8. Finally, restore your data from backup with:

    /usr/local/pgsql/bin/psql -d postgres -f outputfile
    

    using the new psql.

The least downtime can be achieved by installing the new server in a different directory and running both the old and the new servers in parallel, on different ports. Then you can use something like:

pg_dumpall -p 5432 | psql -d postgres -p 5433

to transfer your data.

18.6.2. Upgrading Data via pg_upgrade

The pg_upgrade module allows an installation to be migrated in-place from one major Postgres Pro version to another. Upgrades can be performed in minutes, particularly with --link mode. It requires steps similar to pg_dumpall above, e.g., starting/stopping the server, running initdb. The pg_upgrade documentation outlines the necessary steps.

18.6.3. Upgrading Data via Replication

It is also possible to use logical replication methods to create a standby server with the updated version of Postgres Pro. This is possible because logical replication supports replication between different major versions of Postgres Pro. The standby can be on the same computer or a different computer. Once it has synced up with the primary server (running the older version of Postgres Pro), you can switch primaries and make the standby the primary and shut down the older database instance. Such a switch-over results in only several seconds of downtime for an upgrade.

This method of upgrading can be performed using the built-in logical replication facilities as well as using external logical replication systems such as pglogical, Slony, Londiste, and Bucardo.