postgresql_16:4.4.6.1 Upgrading Data via pg_dumpall
PostgreSQL 16 > Server Administration > Server Setup and Operation > Upgrading a PostgreSQL Cluster > Upgrading Data via pg_dumpall
One upgrade method is to dump data from one major version of PostgreSQL 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 PostgreSQL, 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 PostgreSQL, 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 9.2.
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.
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 (see postgresql_16:4.6) 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 (see postgresql_16:4.11.1.2) for more details. For best results, however, try to use the pg_dumpall command from PostgreSQL 16.15, 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.
Shut down the old server: pg_ctl stop On systems that have PostgreSQL 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 (see postgresql_16:4.4) for details about starting and stopping the server.
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.)
Install the new version of PostgreSQL as outlined in (see postgresql_16:4.2).
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
Restore your previous pg_hba.conf and any postgresql.conf modifications.
Start the database server, again using the special database user account: /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
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.