HOWTO: PostgreSQL version upgrade: различия между версиями

Материал из WiKi - UserSide
Нет описания правки
Нет описания правки
 
(не показано 8 промежуточных версий 2 участников)
Строка 1: Строка 1:
[[HOWTO:_PostgreSQL_version_upgrade|en]] | [[HOWTO:_Обновление_версии_PostgreSQL|ru]]
[[HOWTO:_PostgreSQL_version_upgrade|en]] | [[HOWTO:_Обновление_версии_PostgreSQL|ru]]


This section describes how to upgrade PostgreSQL server to a new major version (e.g. from 9.x to 11.x). You can also upgrade to the maximum version available at the time of the upgrade - they are all backwards compatible.
This section describes the procedure for upgrading PostgreSQL ''between major versions'' (for example, from version 11 to version 17).


We recommend that the server upgrade procedure is performed by a qualified person using the official instructions: https://postgrespro.ru/docs/postgresql/11/upgrading.
An upgrade within a version (for example, from version 17.1 to 17.2) ''happens automatically and does not require additional actions''.


Updating within a major version (e.g. from 11.1 to 11.3) is done without any additional steps.
'''We recommend that you familiarize yourself with the official upgrade instructions''' on the [https://www.postgresql.org/docs/current/upgrading.html postgresql.org] website.


There are several ways of upgrading. Here we will cover the method with a full dump before the upgrade and then a recovery from the dump after the upgrade. The procedure will also be explained for Debian and its derivatives (Ubuntu etc.). For other distributions and operating systems the procedure is almost exactly the same, but other tools specific to a particular distribution or operating system are used.
In the Debian distribution and those based on it, there is a [https://packages.debian.org/sid/postgresql-common postgresql-common] package, which is installed automatically when the PostgreSQL package is installed. The postgresql-common package includes tools that simplify working with PostgreSQL database clusters, including upgrading DB clusters.


It is assumed that PostgreSQL was installed following these instructions and PostgreSQL repositories were added to the system, providing access to different versions of PostgreSQL and extensions, and that the installation followed these instructions and installed the ''postgresql-9.6'' package rather than the ''postgresql metapackage''. If you have a metapackage installed, the upgrade should happen automatically when you upgrade the metapackage, but it is not certain that it will go through correctly. We recommend using specific packages (specifying the version) instead of metapackages.
A database cluster ([https://www.postgresql.org/docs/current/glossary.html Database cluster]) in PostgreSQL refers to a server instance along with all the databases it serves, and not a distributed system as one might think.


If the upgrade process fails to successfully execute any command, please do not continue with the remaining items until the problem has been resolved. Unfortunately, we are not experts in all problems of PostgreSQL and the other services, so we will not always be able to give a qualified answer.
The following example will show how to upgrade a DB cluster from version 11 to version 17 using the tools from the '''postgresql-common''' package.


1. Stop all database-using systems from running. Stop php-fpm or apache2, and temporarily disable tasks in cron and stop usm_* modules from running.
== Upgrade procedure ==
# Stop USERSIDE.
# Upgrade the current version of the postgresql package within its major version (e.g., from 11.5 to 11.22).
# Upgrade the postgis package version to the latest version (e.g., from 2.5 to 3.3.4).
# Update the PostGIS extension in all databases of the cluster.
# Install the new version of the postgresql package without removing the old one (e.g., 17.1).
# Make a backup copy of the database and settings.
# Delete the DB cluster that was created automatically by the script during the new version installation.
# Perform the upgrade of the old version DB cluster to the new version cluster.
# Check if USERSIDE is working correctly.
# Delete the old version DB cluster, obsolete packages, and backup copies.


2. Check which versions of PostgreSQL and PostGIS you currently have installed:
Set aside time for this operation. It can take quite a long time depending on the size of the database and the server's performance. Also, make sure you have enough free disk space, which will be needed for the cluster upgrade (at least as much as the current cluster occupies, but more is better).
sudo -u postgres psql -d userside -A -c "SELECT VERSION()"
sudo -u postgres psql -d userside -A -c "SELECT PostGIS_full_version()"


3. Upgrade the current versions, and if you have PostGIS 2.4 installed, install PostGIS 2.5 and upgrade this extension in the database, then make sure the extension in the database is upgraded:
[https://asciinema.org/a/do2AE0z30mg5JLIuI9zhGTPoR Watch the video instruction on asciinema].
sudo apt-get update
sudo apt-get --only-upgrade install postgresql-9.6 postgresql-9.6-postgis-2.4 -y
sudo apt-get install postgresql-9.6-postgis-2.5 -y
sudo -u postgres psql -d userside -A -c "ALTER EXTENSION postgis UPDATE"
sudo -u postgres psql -d postgres -A -c "ALTER EXTENSION postgis UPDATE"
sudo -u postgres psql -d userside -A -c "SELECT PostGIS_full_version()"
sudo -u postgres psql -d postgres -A -c "SELECT PostGIS_full_version()"


4. Install the new version of PostgreSQL and PostGIS without removing the old one(!).
== Step-by-step instructions ==
sudo apt-get install postgresql-11 postgresql-11-postgis-2.5 -y
The SQL queries with the <code>SELECT</code> operator provided below are for reference to determine the currently installed versions.


5. Perform a backup of all server databases (in this case the latest version of the pg_dumpall utility will be used, but the database will still be used as before):
=== Stop USERSIDE ===
sudo -u postgres pg_dumpall > /tmp/dump_9.6_before_update.dump
<pre>
sudo supervisorctl stop all
sudo systemctl stop php8.3-fpm
</pre>
The PHP version is given as an example. Use the one that is used on your system.


6. Look at how your clusters look. You should now see two versions, both in an online state:
Comment out the startup of all modules and userside in crontab. If you are using [[Usm_poller_(as_satellite)_EN|satellites]], stop the services on them as well.
pg_lsclusters
You should see a table:
Ver Cluster Port Status Owner    Data directory
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main
11  main    5433 online postgres /var/lib/postgresql/11/main


7. When installing PostgreSQL, the installer automatically creates a cluster with the configuration and databases so that you can start working with the database immediately. In case of an upgrade this is an unnecessary operation and the cluster for version 11 needs to be removed. We will create it based on the version 9.6 cluster:
=== Upgrade the current postgresql version within the major version ===
sudo pg_dropcluster 11 main --stop
Check the current versions of the PostgreSQL and PostGIS system packages. Upgrade the currently installed versions of PostgreSQL and PostGIS to the latest versions within their major versions, and also, if the current version of PostGIS is below 3, additionally install the PostGIS 3 package for your version of PostgreSQL. If you already have PostGIS 3 installed, you only need to update it to the latest version.
<pre>
# get the postgresql version and the postgis extension version for the userside database
sudo -u postgres psql -A -t -c "SELECT VERSION()"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"


8. Stop the service
# update the current package versions
sudo systemctl stop postgresql
sudo apt update
sudo apt install --only-upgrade -y postgresql-11 postgresql-11-postgis-2.5
# only if the current postgis version is lower than 3, additionally install postgis-3 for the current postgres version
sudo apt install -y postgresql-11-postgis-3
</pre>
Instead of 11 and 2.5, of course, specify the versions that are actually installed on your system.


9. Make a backup of the current directories with database files and configuration files. Be careful, the database files may take up a lot of storage space, so it may be best to choose a different location for them. Later, if the upgrade is successful, these directories will have to be removed:
=== Upgrade the postgis extension to the latest version ===
sudo cp -r /var/lib/postgresql/9.6/main/ /tmp/pg9.6-lib
After updating the postgis package, you must accordingly update the postgis extension itself in the databases where this extension is present. You may have, for example, a copy of the userside database for testing - do not forget about it.
sudo cp -r /etc/postgresql/9.6/main/ /tmp/pg9.6-etc
<pre>
sudo -u postgres psql -d userside -A -t -c "ALTER EXTENSION postgis UPDATE"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"
</pre>
The second command will display the current version of the PostGIS extension after the update.


10. Start the procedure to create a new cluster based on the old cluster (cluster version upgrade):
=== Upgrading from PostGIS 2.5 to PostGIS 3.x ===
sudo pg_upgradecluster -m upgrade 9.6 main
If the previous <code>SELECT PostGIS_full_version()</code> query displays information like this:
<pre>
POSTGIS="3.3.4 3.3.4" [EXTENSION] . . .
. . .
[UNPACKAGED!] (raster procs from "2.5.5 r0" need upgrade)
</pre>
Then the following information is for you. Otherwise, skip this section.


11. Start the service:
There is some difference between PosGIS versions 2 and 3 that requires additional actions during the upgrade.
sudo systemctl start postgresql
<pre>
# the same query twice
sudo -u postgres psql -d userside -A -t -c "SELECT postgis_extensions_upgrade()"
sudo -u postgres psql -d userside -A -t -c "SELECT postgis_extensions_upgrade()"
# remove the raster extension, which was previously part of the postgis extension
sudo -u postgres psql -d userside -A -t -c "DROP EXTENSION postgis_raster"
</pre>


12. Look at how your clusters look:
=== Install new package versions ===
pg_lsclusters
Also, remove the old postgis version 2 package if it was installed.
Now you should see a table like this one, with version 9.6 in the down state:
<pre>
Ver Cluster Port Status Owner    Data directory
sudo apt install -y postgresql-17 postgresql-17-postgis-3
9.6 main    5433 down  postgres /var/lib/postgresql/9.6/main
sudo apt purge -y postgresql-11-postgis-2.5
11  main    5432 online postgres /var/lib/postgresql/11/main
</pre>


13. Check the versions:
=== Make a backup copy of the database and configuration ===
sudo -u postgres psql -d userside -A -c "SELECT VERSION()"
The cluster migration tool does this absolutely safely, but extra caution will not be superfluous. Make temporary backup copies of the necessary databases and configuration, which can be safely deleted at the end of the update.
sudo -u postgres psql -d userside -A -c "SELECT PostGIS_full_version()"
<pre>
sudo -u postgres pg_dump --no-acl -Fp -Z 5 userside --file=/backup/userside_emergency.sql.gz
sudo -u postgres pg_dump --no-acl -Fc userside --file=/backup/userside_emergency.dump
sudo tar czf /backup/postgres_config_emergency.tgz -C /etc/postgresql/11/main .
</pre>
The /backup directory must exist and must be writable by all users.


14. Run all the services you stopped in step 1 and check that USERSIDE and modules are working. Check that everything is working correctly.
=== Delete the automatically created DB cluster ===
Using the <code>pg_lsclusters</code> utility from postgresql-common, see what your DB clusters look like:
<pre>
sudo pg_lsclusters
</pre>
The command output will be approximately as follows:
<pre>
Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11.log
17  main    5433 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17.log
</pre>
The 17/main cluster was created automatically by the postgresql installation script and uses the new port 5433. It needs to be stopped and deleted — it is not needed, as a cluster migration procedure will be performed, which will create a new cluster automatically.
<pre>
sudo pg_dropcluster 17 main --stop
</pre>
Running the <code>pg_lsclusters</code> command again should show that you have one cluster left, '''11/main'''.


15. After you have ensured that everything works correctly, you can remove the temporary backups as well as the old packages:
=== Perform the upgrade of the old version DB cluster to the new version cluster ===
  sudo pg_dropcluster 9.6 main --stop
Stop the postgresql service
  sudo apt-get purge postgresql-9.6 postgresql-9.6-postgis-2.4 postgresql-9.6-postgis-2.5 -y
<pre>
sudo apt autoremove -y
sudo systemctl stop postgresql
sudo rm -rf /tmp/pg9.6-* /tmp/dump_9.6_before_update.dump
</pre>
Start the DB cluster '''11/main''' upgrade process using the <code>pg_upgradecluster</code> utility from the postgresql-common package. This action may take a lot of time and disk space.
<pre>
sudo pg_upgradecluster 11 main
</pre>
After the upgrade operation is complete, you will see the new state of the clusters (the old version should be in '''down''' mode and port 5432 is now assigned to the new cluster '''17/main'''):
<pre>
Ver Cluster Port Status Owner    Data directory              Log file
11 main   5433 down  postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11.log
17 main    5432 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17.log
</pre>
Start the postgres service
<pre>
sudo systemctl start postgresql
</pre>


PostgreSQL upgrade completed.
=== Check USERSIDE operation ===
First, run the commands that display the versions to make sure that the database is now running on the new version DB cluster:
<pre>
sudo -u postgres psql -A -t -c "SELECT VERSION()"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"
</pre>
You should see postgresql version 17 and postgis version 3.x.x.


Video guide: https://youtu.be/c9ULVSz8yQM
Now start the FPM service and go to USERSIDE through a browser.
<pre>
sudo systemctl start php8.3-fpm
</pre>
Since other services are stopped, you will probably see errors related to them, but the USERSIDE interface should open without database connection errors.
If everything is successful, then uncomment the previously commented lines in crontab and start the external services managed by supervisor, including on the satellite:
<pre>
sudo supervisorctl start all
sudo supervisorctl status
</pre>
 
=== Cleanup after a successful upgrade ===
If USERSIDE is working correctly and there are no database interaction errors, you can delete the old cluster and packages. You can also, if you wish, delete the emergency backups or wait a few days to be absolutely sure they will not be needed.
<pre>
sudo pg_dropcluster 11 main --stop
sudo apt purge -y postgresql-11 postgresql-11-postgis-3
sudo apt autoremove -y
sudo rm /backup/userside_emergency* /backup/postgres_config_emergency.tgz
sudo rm -rf /var/lib/postgresql/11/main
</pre>

Текущая версия от 06:11, 21 августа 2025

en | ru

This section describes the procedure for upgrading PostgreSQL between major versions (for example, from version 11 to version 17).

An upgrade within a version (for example, from version 17.1 to 17.2) happens automatically and does not require additional actions.

We recommend that you familiarize yourself with the official upgrade instructions on the postgresql.org website.

In the Debian distribution and those based on it, there is a postgresql-common package, which is installed automatically when the PostgreSQL package is installed. The postgresql-common package includes tools that simplify working with PostgreSQL database clusters, including upgrading DB clusters.

A database cluster (Database cluster) in PostgreSQL refers to a server instance along with all the databases it serves, and not a distributed system as one might think.

The following example will show how to upgrade a DB cluster from version 11 to version 17 using the tools from the postgresql-common package.

Upgrade procedure

  1. Stop USERSIDE.
  2. Upgrade the current version of the postgresql package within its major version (e.g., from 11.5 to 11.22).
  3. Upgrade the postgis package version to the latest version (e.g., from 2.5 to 3.3.4).
  4. Update the PostGIS extension in all databases of the cluster.
  5. Install the new version of the postgresql package without removing the old one (e.g., 17.1).
  6. Make a backup copy of the database and settings.
  7. Delete the DB cluster that was created automatically by the script during the new version installation.
  8. Perform the upgrade of the old version DB cluster to the new version cluster.
  9. Check if USERSIDE is working correctly.
  10. Delete the old version DB cluster, obsolete packages, and backup copies.

Set aside time for this operation. It can take quite a long time depending on the size of the database and the server's performance. Also, make sure you have enough free disk space, which will be needed for the cluster upgrade (at least as much as the current cluster occupies, but more is better).

Watch the video instruction on asciinema.

Step-by-step instructions

The SQL queries with the SELECT operator provided below are for reference to determine the currently installed versions.

Stop USERSIDE

sudo supervisorctl stop all
sudo systemctl stop php8.3-fpm

The PHP version is given as an example. Use the one that is used on your system.

Comment out the startup of all modules and userside in crontab. If you are using satellites, stop the services on them as well.

Upgrade the current postgresql version within the major version

Check the current versions of the PostgreSQL and PostGIS system packages. Upgrade the currently installed versions of PostgreSQL and PostGIS to the latest versions within their major versions, and also, if the current version of PostGIS is below 3, additionally install the PostGIS 3 package for your version of PostgreSQL. If you already have PostGIS 3 installed, you only need to update it to the latest version.

# get the postgresql version and the postgis extension version for the userside database
sudo -u postgres psql -A -t -c "SELECT VERSION()"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"

# update the current package versions
sudo apt update
sudo apt install --only-upgrade -y postgresql-11 postgresql-11-postgis-2.5
# only if the current postgis version is lower than 3, additionally install postgis-3 for the current postgres version
sudo apt install -y postgresql-11-postgis-3

Instead of 11 and 2.5, of course, specify the versions that are actually installed on your system.

Upgrade the postgis extension to the latest version

After updating the postgis package, you must accordingly update the postgis extension itself in the databases where this extension is present. You may have, for example, a copy of the userside database for testing - do not forget about it.

sudo -u postgres psql -d userside -A -t -c "ALTER EXTENSION postgis UPDATE"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"

The second command will display the current version of the PostGIS extension after the update.

Upgrading from PostGIS 2.5 to PostGIS 3.x

If the previous SELECT PostGIS_full_version() query displays information like this:

POSTGIS="3.3.4 3.3.4" [EXTENSION] . . .
. . .
[UNPACKAGED!] (raster procs from "2.5.5 r0" need upgrade)

Then the following information is for you. Otherwise, skip this section.

There is some difference between PosGIS versions 2 and 3 that requires additional actions during the upgrade.

# the same query twice
sudo -u postgres psql -d userside -A -t -c "SELECT postgis_extensions_upgrade()"
sudo -u postgres psql -d userside -A -t -c "SELECT postgis_extensions_upgrade()"
# remove the raster extension, which was previously part of the postgis extension
sudo -u postgres psql -d userside -A -t -c "DROP EXTENSION postgis_raster"

Install new package versions

Also, remove the old postgis version 2 package if it was installed.

sudo apt install -y postgresql-17 postgresql-17-postgis-3
sudo apt purge -y postgresql-11-postgis-2.5

Make a backup copy of the database and configuration

The cluster migration tool does this absolutely safely, but extra caution will not be superfluous. Make temporary backup copies of the necessary databases and configuration, which can be safely deleted at the end of the update.

sudo -u postgres pg_dump --no-acl -Fp -Z 5 userside --file=/backup/userside_emergency.sql.gz
sudo -u postgres pg_dump --no-acl -Fc userside --file=/backup/userside_emergency.dump
sudo tar czf /backup/postgres_config_emergency.tgz -C /etc/postgresql/11/main .

The /backup directory must exist and must be writable by all users.

Delete the automatically created DB cluster

Using the pg_lsclusters utility from postgresql-common, see what your DB clusters look like:

sudo pg_lsclusters

The command output will be approximately as follows:

Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11.log
17  main    5433 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17.log

The 17/main cluster was created automatically by the postgresql installation script and uses the new port 5433. It needs to be stopped and deleted — it is not needed, as a cluster migration procedure will be performed, which will create a new cluster automatically.

sudo pg_dropcluster 17 main --stop

Running the pg_lsclusters command again should show that you have one cluster left, 11/main.

Perform the upgrade of the old version DB cluster to the new version cluster

Stop the postgresql service

sudo systemctl stop postgresql

Start the DB cluster 11/main upgrade process using the pg_upgradecluster utility from the postgresql-common package. This action may take a lot of time and disk space.

sudo pg_upgradecluster 11 main

After the upgrade operation is complete, you will see the new state of the clusters (the old version should be in down mode and port 5432 is now assigned to the new cluster 17/main):

Ver Cluster Port Status Owner    Data directory              Log file
11  main    5433 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11.log
17  main    5432 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17.log

Start the postgres service

sudo systemctl start postgresql

Check USERSIDE operation

First, run the commands that display the versions to make sure that the database is now running on the new version DB cluster:

sudo -u postgres psql -A -t -c "SELECT VERSION()"
sudo -u postgres psql -d userside -A -t -c "SELECT PostGIS_full_version()"

You should see postgresql version 17 and postgis version 3.x.x.

Now start the FPM service and go to USERSIDE through a browser.

sudo systemctl start php8.3-fpm

Since other services are stopped, you will probably see errors related to them, but the USERSIDE interface should open without database connection errors. If everything is successful, then uncomment the previously commented lines in crontab and start the external services managed by supervisor, including on the satellite:

sudo supervisorctl start all
sudo supervisorctl status

Cleanup after a successful upgrade

If USERSIDE is working correctly and there are no database interaction errors, you can delete the old cluster and packages. You can also, if you wish, delete the emergency backups or wait a few days to be absolutely sure they will not be needed.

sudo pg_dropcluster 11 main --stop
sudo apt purge -y postgresql-11 postgresql-11-postgis-3
sudo apt autoremove -y
sudo rm /backup/userside_emergency* /backup/postgres_config_emergency.tgz
sudo rm -rf /var/lib/postgresql/11/main