pg_upgrade is a PostgreSQL utility that facilitates the in-place upgrade of a PostgreSQL database cluster to a new major version. It allows users to upgrade their database without needing to dump and restore the database, significantly reducing downtime. Here are the key steps involved in using pg_upgrade:
Preparation: Before starting the upgrade, ensure both the old and new versions of PostgreSQL are installed. Backup the existing database cluster and ensure no connections are active.
Initialize the New Cluster: Initialize a new PostgreSQL cluster with the target version using initdb.
Run pg_upgrade: Execute the pg_upgrade command, specifying the data directories of the old and new clusters, and the paths to the old and new pg_ctl binaries.
Analyze and Optimize: After the upgrade, run the analyze_new_cluster.sh script generated by pg_upgrade to update optimizer statistics. This step is crucial for performance.
Finalize: If everything works correctly, you can start the new cluster and remove the old cluster to free up space.
Learn more from the following resources: