When updating PostgreSQL to a major version, the utility pg_upgrade is used. It allows updating the data stored in PostgreSQL data files to a later version of PostgreSQL.
The update of standalone PostgreSQL consists of six steps:
- Database backup.
- Installation of PostgreSQL 18.
- Updating data in the database directory.
- Configuration update.
- Post-installation check.
Step 1. Database backup
Before performing any actions related to the update, create a backup of the data and PostgreSQL configuration. Read more about how to do this in Backup and recover databases.
начало внимание
If there are third-party databases on the PostgreSQL server, their backups also need to be created.
конец внимание
Backup of the PostgreSQL database without using the elma365-backupper utility
sudo -u postgres pg_dump elma365 -Fc > path/to/elma365.bak |
Step 2. Installation of PostgreSQL 18
Use the package manager of your operating system to install PostgreSQL 18 on the server where version 12 or lower is installed:
sudo apt install postgresql-18
Step 3. Updating data in the database directory
- Stop PostgreSQL to keep its data inaccessible during the update. In this example version 12 is updated to version 18:
sudo systemctl stop postgresql@12 postgresql@18
- Ensure that both PostgreSQL 12 or lower and PostgreSQL 18 clusters are stopped:
pg_lsclusters
- On the server, perform a check for database configuration compatibility:
sudo -iu postgres /usr/lib/postgresql/18/bin/pg_upgrade \
-o "-c config_file=/etc/postgresql/12/main/postgresql.conf" \
-O "-c config_file=/etc/postgresql/18/main/postgresql.conf" \
--old-datadir=/var/lib/postgresql/12/main/ \
--new-datadir=/var/lib/postgresql/18/main/ \
--old-bindir=/usr/lib/postgresql/12/bin \
--new-bindir=/usr/lib/postgresql/18/bin --check
Where:
/usr/lib/postgresql/12/binand/usr/lib/postgresql/18/binare the paths to the binary files of PostgreSQL versions 12 and 18./var/lib/postgresql/12/mainand/var/lib/postgresql/18/mainare the paths to the data directories.
- On the server, perform the transfer of data from PostgreSQL 12 or lower to PostgreSQL 18:
sudo -iu postgres /usr/lib/postgresql/18/bin/pg_upgrade \
-o "-c config_file=/etc/postgresql/12/main/postgresql.conf" \
-O "-c config_file=/etc/postgresql/18/main/postgresql.conf" \
--old-datadir=/var/lib/postgresql/12/main/ \
--new-datadir=/var/lib/postgresql/18/main/ \
--old-bindir=/usr/lib/postgresql/12/bin \
--new-bindir=/usr/lib/postgresql/18/bin
Where:
/usr/lib/postgresql/12/binand/usr/lib/postgresql/18/binare the paths to the binary files of PostgreSQL versions 12 and 18./var/lib/postgresql/12/mainand/var/lib/postgresql/18/mainare the paths to the data directories.
Step 4. Configuration update
- Update the PostgreSQL 18 configuration settings according to the configuration used in PostgreSQL 12 or lower:
sudo nano /etc/postgresql/18/main/postgresql.conf
sudo nano /etc/postgresql/18/main/pg_hba.conf
- Run PostgreSQL 18:
sudo systemctl start postgresql@18
Step 5. Post-installation check
- Check PostgreSQL database status:
sudo systemctl status postgresql
- Ensure that PostgreSQL server is ready to receive connections from clients:
sudo pg_isready
Found a typo? Select it and press Ctrl+Enter to send us feedback