Upgrade PostgreSQL 9.5 to 16 on Linux
Upgrade PostgreSQL 9.5 to 16 on Linux
In the postgresql.conf file, changes include setting listen_addresses to '*', port to 5432, enabling archive_mode, specifying an archive_command for rsync operations, and setting wal_level to replica. In pg_hba.conf, replication privileges must be configured to allow access for the replication user from the standby server's IP address, requiring an md5 authentication method. These changes ensure the system is prepared for secure and consistent streaming replication .
The process involves several key steps: collecting information about existing database tables, tablespaces, and users; moving the old PostgreSQL cluster directory; installing the new PostgreSQL version; initializing the new database cluster; stopping both the old and new clusters while ensuring streaming replication servers are running; running pg_upgrade with checks and using hard links to upgrade the data; upgrading the standby servers; configuring the streaming replication and log-shipping on both primary and standby servers; starting the upgraded PostgreSQL services and verifying replication status; and finally cleaning up by deleting the old cluster directories .
Stopping both the old and new PostgreSQL clusters while keeping streaming replication servers running is crucial to ensure that all changes are captured and transferred from the primary to the standby, maintaining synchronicity between their states. This practice prevents data discrepancies during the transition and guarantees that when operations resume post-upgrade, both servers are starting from a consistent and synchronized data state .
Deleting the old cluster files is important to free up storage space and avoid confusion or unintended connection to the obsolete data. Once the new PostgreSQL setup is confirmed operational and stable, retaining old data clusters could pose a risk of data inconsistency, unnecessary resource usage, and potential security vulnerabilities .
Using hard links instead of copying files during an upgrade with pg_upgrade is recommended because it improves performance by avoiding the need to duplicate data, thereby reducing the time taken for the upgrade and the additional storage required .
When installing new PostgreSQL binaries on standby servers, it is critical to ensure that any existing data directories related to the standby setup are removed or empty. Running the initdb process or deploying new files to existing non-empty directories could lead to data inconsistency or corruption. Therefore, prior to configuring the new setup, execute a recursive delete command on the new data directories to guarantee that they are in the proper initial state for receiving updated data from the primary server .
Rsync is used to quickly upgrade the standby server by synchronizing the new data directories from the primary server. It uses options that ensure efficient and consistent file transfer such as --archive, --delete, --hard-links, and --no-inc-recursive. This approach minimizes downtime and ensures that the standby server's data directories are accurately updated to reflect the state of the primary server .
Enabling archive_mode in the postgresql.conf file allows the system to archive write-ahead logs (WAL) which can then be used for log-shipping. This is crucial for maintaining consistent replication states between the primary and standby servers and ensuring that all transactions are securely documented and can be reflected on the standby in case of a failover .
The status of PostgreSQL replication can be verified by executing queries on the primary database instance to check the data in pg_stat_replication, and on the standby database instance to view pg_stat_wal_receiver. These queries provide insight into the current state of replication, including the connection status and lag between the primary and standby servers .
Step 9 is crucial as it involves configuring the new standby server for streaming replication, ensuring it can effectively communicate with the primary server. This includes touching the standby.signal file to initiate standby behavior, editing postgresql.conf to specify connection details, and setting up the restore_command to facilitate communication during replication. These steps ensure the standby server is correctly aligned with the operational requirements of the upgraded PostgreSQL environment, allowing it to seamlessly take over in case of primary server failure .