• Post author:

PostgreSQL is a powerful, open-source relational database management system that undergoes regular updates to enhance performance, security, and features. Upgrading your PostgreSQL database from version 10 to version 13 is a crucial step to ensure you benefit from the latest improvements and optimizations. In this blog post, I’ll walk you through a detailed, step-by-step guide to help you smoothly upgrade your PostgreSQL database.

**Disclaimer:** Before starting the upgrade process, make sure to back up your database to prevent data loss in case of unexpected issues. Also, perform these steps on a test environment first to mitigate any potential risks.

Review Release Notes

Before initiating the upgrade process, carefully review the release notes for PostgreSQL versions 11, 12, and 13. This will give you insights into the changes, new features, and any potential compatibility issues that may affect your specific use case.

Link: https://www.postgresql.org/docs/current/pgupgrade.html

Why Upgrade Matters

1️⃣ Performance Boost: One of the top reasons to keep your PostgreSQL database up to date is the performance boost that comes with each new release. Developers work tirelessly to enhance query execution, optimize resource utilization, and introduce new features that can significantly improve your database’s overall performance. Upgrade to experience the speed and efficiency gains firsthand! ⚡

2️⃣ Security Reinforcement: Security is paramount in the digital age, and your database is no exception. Each PostgreSQL upgrade comes with security patches and bug fixes that address vulnerabilities discovered in previous versions. By staying current, you’re not just keeping up with the latest features but also fortifying your database against potential threats. 🛡️

3️⃣ Feature Richness: PostgreSQL evolves with every release, introducing new features and capabilities. Whether it’s advanced indexing options, enhanced support for data types, or improved SQL functionalities, upgrading ensures you have access to the latest tools to meet the evolving demands of your applications. Stay ahead of the curve by leveraging the richness of the latest PostgreSQL features! 🌐✨

4️⃣ Compatibility and Interoperability: As the database landscape evolves, so do the standards and protocols. Upgrading PostgreSQL ensures compatibility with the latest industry standards and enhances interoperability with other technologies. This is crucial for seamless integration with new tools and services in your tech stack. 🔄

5️⃣ Community Support: The PostgreSQL community is vibrant and active. By staying on the latest version, you benefit from ongoing community support, ensuring that you have access to resources, forums, and expert advice when you need it most. The collective wisdom of the community can be a game-changer in overcoming challenges and optimizing your database. 👥💬

Backup the Existing Database
Begin by creating a comprehensive backup of your PostgreSQL 10 database. This ensures that you have a safe copy of your data to revert to in case anything goes wrong during the upgrade process.

Connect to the PostgreSQL server

# psql -U <username> -d <database>

Run a PG dump of the database for backup

# pg_dump -U <username> -d <database> -f backup_file.sql
Example: /usr/pgsql-10/bin/pg_dump -U postgres -d scm | gzip > scm_backup.sql.gz

I have created a script to take full backup of the database. You can access this script from my github account link is given below:

https://github.com/prashant-raghava/postgres-backup

Run this script to take backup of all schema’s 

# ./pg_backup.sh
Exit

# \q

Stop PostgreSQL Service 10 on existing host 

# pg_ctl stop -D /path/to/pgsql/data

Install PostgreSQL 13
Install PostgreSQL 13 on your server. Depending on your operating system, you can use package managers like `apt`, `yum`, or download the binaries from the official PostgreSQL website.

Add PostgreSQL repository
# sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# sudo yum install postgresql13-server*
Initialize and Start PostgreSQL 13
# sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
Start Postgres service
# /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data start

By default it create directory under /var/lib/pgsql/13. I have created the link file for 13 version on server. In this directory data directory will be created . Verify under the below directory data directory created or not.

/var/lib/pgsql/13 --> /postgres/pgsql/13

Update PostgreSQL Configuration (if necessary)
Update the PostgreSQL 13 configuration files based on your old configuration files. Pay attention to parameters that may have changed between versions. Check and update your PostgreSQL configuration files (e.g., `postgresql.conf` and `pg_hba.conf`) based on any changes between versions.
Note : Before upgrade, current version and new version postgres service should be stopped state.

Stop PostgreSQL Service

# /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data status
# /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data stop
or

# pg_ctl start -D /path/to/pgsql-13/data

Enable PostgreSQL 13 to start on boot

# /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data enable
 or
# sudo systemctl enable postgresql-13

Upgrade postgres
We will use following command for upgrade

pg_upgrade -b oldbindir [-B newbindir] -d oldconfigdir -D newconfigdir
Upgrade command :
/usr/pgsql-13/bin/pg_upgrade \
    -b /usr/pgsql-10/bin/ \
    -B /usr/pgsql-13/bin/ \
    -d /var/lib/pgsql/10/data \
    -D /var/lib/pgsql/13/data

Execute command with -c option to verify

# /usr/pgsql-13/bin/pg_upgrade -b /usr/pgsql-10/bin/ -B /usr/pgsql-13/bin/ -d /var/lib/pgsql/10/data -D /var/lib/pgsql/13/data -c
Out put:
Performing Consistency Checks

-----------------------------
Checking cluster versions ok
Checking database user is the install user ok
Checking database connection settings ok
Checking for prepared transactions ok
Checking for system-defined composite types in user tables ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
Checking for new cluster tablespace directories ok

*Clusters are compatible*

Once everything works fine execute command withour -c option

/usr/pgsql-13/bin/pg_upgrade -b /usr/pgsql-10/bin/ -B /usr/pgsql-13/bin/ -d /var/lib/pgsql/10/data -D /var/lib/pgsql/13/data

Test and Validate
Run thorough tests on your upgraded PostgreSQL 13 database to ensure everything is functioning as expected. Validate your applications, perform queries, and check for any errors or inconsistencies.

Update Statistics
Run the `ANALYZE` command on your databases to update statistics, which helps the query planner make better decisions.

# psql -U <username> -d <database_name> -c 'ANALYZE;'
or
# ./analyze_new_cluster.sh

Monitor Performance
Monitor the performance of your PostgreSQL 13 database, keeping an eye on resource utilization, query execution times, and system logs.

Restore the database from the backup:

# psql -U <username> -d <database> -f backup_file.sql
# \q

Post-upgrade best practices for a PostgreSQL database:

1. Conduct Thorough Testing:
– After completing the upgrade, thoroughly test your applications to ensure they function correctly with the new PostgreSQL version.
– Perform both functional and performance testing to identify any potential issues or bottlenecks.

2. Monitor Database Performance:
– Implement monitoring tools to keep a close eye on the performance of your PostgreSQL database.
– Monitor key metrics such as query execution times, resource utilization, and overall system health.

3. Review and Optimize Queries:
– Take the opportunity to review and optimize SQL queries. New PostgreSQL versions may introduce query optimization features, and revisiting queries can lead to improved performance.

4. Update Statistics and Analyze:
– Update database statistics and run the `ANALYZE` command to ensure the query planner has accurate information for optimizing query plans.

5. Backup Database Regularly:
– Schedule regular backups of your PostgreSQL database to prevent data loss in case of unforeseen issues.
– Verify the integrity of your backups to ensure they can be successfully restored if needed.

6. Document Changes and New Features:
– Document any changes made during the upgrade process, including configuration adjustments and new features introduced in the upgraded PostgreSQL version.

7. Review and Adjust Configuration Settings:
– Evaluate PostgreSQL configuration settings and adjust them based on the workload and performance requirements of your specific environment.

8. Monitor Disk Space Usage:
– Keep a close watch on disk space usage, especially if the upgrade involves changes in storage requirements or if there are new features impacting disk usage.

9. Implement Regular Maintenance Tasks:
– Schedule regular maintenance tasks such as vacuuming and analyzing to optimize database performance and reclaim storage space.

10. Stay Informed about Updates:
– Stay informed about future updates and patches for the PostgreSQL version you’ve upgraded to. Regularly apply relevant updates to benefit from bug fixes and security patches.

11. Check for Deprecated Features:
– Be aware of any features that have been deprecated in the upgraded PostgreSQL version. Plan for their replacement in future updates to avoid compatibility issues.

12. Educate Your Team:
– Ensure your team is familiar with the changes introduced in the upgraded PostgreSQL version. Provide training if necessary to leverage new features effectively.

13. Engage with the Community:
– Join PostgreSQL forums and communities to stay connected with other users who have undergone similar upgrades. Share experiences and learn from the community.

Remember, each database environment is unique, so adapt these best practices to fit the specific needs and characteristics of your PostgreSQL deployment.

Conclusion:

Congratulations! You’ve successfully upgraded your PostgreSQL database from version 10 to version 13. Regularly updating your database software is essential to benefit from the latest features and improvements while ensuring the security and stability of your data. If you encounter any issues during the upgrade process, refer to the PostgreSQL documentation or seek help from the PostgreSQL community. Happy database upgrading!

Leave a Reply