How to Upgrade PostgreSQL Version in Ubuntu
Why should you upgrade?
1. Community support and Bug fixes : PostgreSQL is one of the most popular open source community database. Every version of PostgreSQL is only supported by the community for a pre-defined period of time. So the bug fixes and patches will be only available for a certain period. Thus PostgreSQL community will not release any fixes or security releases after the EOL (End of Life). Thus new vulnerabilities will not be addressed.
2. Compliance : Because of the strict compliance of the organizations, every application database should be up-to-date.
3. Performance : With every new versions PostgreSQL delivers better performances.
4. Feature / Need : With every new versions PostgreSQL releases new features and improvements.
5. Data Security : Because of the lack of fix and patches, the vulnerabilities increases and there will be a chance of security breaches.
6. Support for new PostgreSQL Extensions : Tools like PGPool and PGBouncer gets better in each versions.
Checking the version of PostgreSQL
sudo dpkg -l | grep postgresql
Install a new PostgreSQL version
Before upgrading we have to install new version of PostgreSQL
sudo apt update && sudo apt upgrade sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main 15" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt update sudo apt-get install postgresql-15
Run the same dpkg command to ensure the installation
sudo dpkg -l | grep postgresql
We can check the status of the clusters by using the below command.
pg_lsclusters
PostgreSQL version 15 cluster "main" is created at installation.
When trying to upgrade our our "main" version 12 will clash with the existing
version 15. So we need to drop the cluster using below command
sudo pg_dropcluster 15 main --stop
Now we can upgrade the "main" cluster from version 12 to version 15
sudo pg_upgradecluster 12 main
We can verify the cluster once more
pg_lsclusters
when everything is running we can finally remove version 12 "main" cluster and
PostgreSQL packages
sudo pg_dropcluster 12 main sudo apt-get purge postgresql-12 postgresql-client-12