Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Upgrading PostgreSQL Versions

Introduction

Explanation: Upgrading PostgreSQL to a newer version is an important task to take advantage of new features, performance improvements, and security patches. This tutorial covers different methods to upgrade PostgreSQL versions, including in-place upgrades and using logical replication.

Pre-Upgrade Preparation

Before upgrading, ensure you have a proper backup of your database and verify the compatibility of your applications with the new PostgreSQL version.

  • Read the release notes for the new version.
  • Test the upgrade process in a staging environment.
  • Ensure you have sufficient disk space for the upgrade process.
  • Perform a full backup of your existing database.

Method 1: In-Place Upgrade Using pg_upgrade

The pg_upgrade tool allows for in-place upgrades with minimal downtime.

Step-by-Step Guide

  1. Install the new PostgreSQL version.
  2. Stop the PostgreSQL server:
  3. Command:

    
    sudo systemctl stop postgresql
                        
  4. Initialize the new database cluster:
  5. Command:

    
    sudo -u postgres /usr/pgsql-13/bin/initdb -D /var/lib/pgsql/13/data
                        
  6. Run pg_upgrade:
  7. Command:

    
    sudo -u postgres /usr/pgsql-13/bin/pg_upgrade \
      -b /usr/pgsql-12/bin -B /usr/pgsql-13/bin \
      -d /var/lib/pgsql/12/data -D /var/lib/pgsql/13/data
                        
  8. Start the new PostgreSQL server:
  9. Command:

    
    sudo systemctl start postgresql-13
                        
  10. Analyze and reindex the database:
  11. Command:

    
    sudo -u postgres /usr/pgsql-13/bin/vacuumdb --all --analyze-in-stages
                        

Method 2: Logical Replication

Logical replication can be used to migrate data from the old PostgreSQL version to the new version with minimal downtime.

Step-by-Step Guide

  1. Set up logical replication on the source database:
  2. SQL:

    
    -- On the source database
    ALTER SYSTEM SET wal_level = logical;
    SELECT pg_reload_conf();
    CREATE PUBLICATION my_pub FOR ALL TABLES;
                        
  3. Set up logical replication on the target database:
  4. SQL:

    
    -- On the target database
    CREATE SUBSCRIPTION my_sub CONNECTION 'host=source_host port=5432 dbname=mydb user=replicator password=secret' PUBLICATION my_pub;
                        
  5. Monitor the replication process:
  6. SQL:

    
    -- On the target database
    SELECT * FROM pg_stat_subscription;
                        
  7. Switch to the new PostgreSQL version after replication is caught up.

Post-Upgrade Steps

After upgrading, perform the following steps:

  • Verify the data integrity and functionality of your applications.
  • Remove the old PostgreSQL binaries and data directory if no longer needed.
  • Update any scripts or configuration files to point to the new PostgreSQL version.

Troubleshooting

If you encounter issues during the upgrade process, refer to the PostgreSQL documentation and logs for troubleshooting. Common issues include:

  • Compatibility issues with extensions or custom data types.
  • Insufficient disk space.
  • Configuration file errors.