Postgresql upgrade from version 9 to 14

Postgresql upgrade from version 9 to 14_第1张图片

 

PostgreSQL: Documentation: 15: 19.6. Upgrading a PostgreSQL Cluster

PostgreSQL: Documentation: 15: pg_upgrade

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.

  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 21 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 26.1.2 for more details. For best results, however, try to use the pg_dumpall command from PostgreSQL 15.3, 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 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 Chapter 19 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 PostgreSQL as outlined in Section 17.4.

  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.

Upgrading Data via pg_upgrade

The pg_upgrade module allows an installation to be migrated in-place from one major PostgreSQL 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.

check the consistency before the upgrade

/usr/pgsql-14/bin/pg_upgrade \
    -b  /usr/bin  \              
    -d  /mnt/x1/pg_data \        
    -D /mnt/x1/pg14_data \       
    -B /usr/pgsql-14/bin  \      
    -c                    

The flags mean:

  • -b: The old PostgreSQL executable directory
  • -B: The new PostgreSQL executable directory
  • -d: The old database cluster configuration directory
  • -D: The new database cluster configuration directory
  • -c: Check clusters only. It doesn’t change any data

Postgresql upgrade from version 9 to 14_第2张图片

If the last step executes without error, you can run the same command without the -c flag and it will upgrade your PostgreSQL server.

/usr/pgsql-14/bin/pg_upgrade \
    -b  /usr/bin  \              # old cluster executable directory
    -d  /mnt/x1/pg_data \        # old cluster data directory
    -D /mnt/x1/pg14_data \       # new cluster data directory
    -B /usr/pgsql-14/bin         # new cluster executable directory
    

Start the New Postgres Server

sudo systemctl restart postgresql-14

Analyze the new cluster and then delete old data

./analyze_new_cluster.sh
./delete_old_cluster.sh

Upgrading Data via Replication

It is also possible to use logical replication methods to create a standby server with the updated version of PostgreSQL. This is possible because logical replication supports replication between different major versions of PostgreSQL. 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 PostgreSQL), 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.

你可能感兴趣的:(数据库,服务器,数据库,pgsql)