A Comprehensive Guide to Upgrading PostgreSQL from 9.6 to 10.1 Without Data Loss

Nov 24, 2025 · Programming · 8 views · 7.8

Keywords: PostgreSQL Upgrade | Data Migration | pg_upgrade

Abstract: This article provides a detailed technical walkthrough for upgrading PostgreSQL from version 9.6 to 10.1 on Mac OS X using Homebrew, focusing on the pg_upgrade tool, data migration strategies, and post-upgrade validation to ensure data integrity and service continuity.

Pre-Upgrade Preparation

Before initiating the PostgreSQL version upgrade, thorough preparation is essential. Ensure the current database service is stable and schedule the operation during low-traffic periods. Backup is the most critical safety measure; use the pg_dump command to create a full database backup: pg_dump -U username -h localhost -d database_name > backup.sql. Verify disk space availability to accommodate both old and new database clusters.

Stopping the Current Service

The upgrade process requires stopping the running PostgreSQL service. For Homebrew-installed PostgreSQL, use the LaunchAgents management system: launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist. This command gracefully stops the PostgreSQL service process, ensuring no active database connections remain.

Initializing the New Database

Initialize the new version database cluster before upgrading. Use the initdb command to create the 10.1 version directory: initdb /usr/local/var/postgres10.1 -E utf8. The -E utf8 parameter sets the default database encoding to UTF-8, ensuring character set compatibility. The new cluster directory should be separate from the old one to avoid file conflicts.

Executing pg_upgrade Migration

pg_upgrade is the official PostgreSQL upgrade tool for migrating data between major versions. The basic command format is: pg_upgrade -v -d /usr/local/var/postgres -D /usr/local/var/postgres10.1 -b /usr/local/Cellar/postgresql/9.6.5/bin/ -B /usr/local/Cellar/postgresql/10.1/bin/. Here, -v enables verbose logging, -d specifies the old cluster directory, -D specifies the new cluster directory, and -b and -B specify the old and new binary paths, respectively.

Directory Switch and Restart

After upgrade completion, perform directory switching: cd /usr/local/var && mv postgres postgres9.6 && mv postgres10.1 postgres. This operation moves the new version cluster to the standard location while retaining the old version as backup. Then restart the service: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist.

Verification and Post-Upgrade Handling

After service restart, verify the upgrade outcome. Check the log file /usr/local/var/postgres/server.log for any error messages. Connect to the database and execute basic queries to validate data integrity. For Ruby on Rails applications, reinstall the pg gem: gem uninstall pg && gem install pg, ensuring compatibility with the new PostgreSQL version client library.

Upgrade Considerations

Potential issues during upgrade include extension compatibility and data type changes. It is advisable to conduct a full upgrade rehearsal in a test environment first. The PostgreSQL official documentation provides detailed upgrade guidelines and troubleshooting methods, which should be thoroughly reviewed before operation. If the upgrade fails, a quick rollback to the backed-up old version cluster is possible.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.