Nextcloud

How to Upgrade PostgreSQL from 14 to 15

This-one-logo-cloud-only9-center-purple-bk-h450-w650.png

By jplee ~ Published – 2022/11/18

The new PostgreSQL 15 database has been released. There are multiple ways to upgrade from the old version 14, and the easiest one is by using the pg_upgrade tool. Here is a quick tutorial for Ubuntu (or Debian) systems. And, please, do not forget to back up your data!

Step 1:  Update packages and install the new PostgreSQL 15 database.

sudo apt update && sudo apt upgrade -y
sudo apt install postgresql-15 postgresql-server-dev-15

Step 2:  Check if there are any differences in the config files.

diff /etc/postgresql/14/main/postgresql.conf /etc/postgresql/15/main/postgresql.conf
diff /etc/postgresql/14/main/pg_hba.conf /etc/postgresql/15/main/pg_hba.conf

Step 3:  Stop the PostgreSQL service.

sudo systemctl stop postgresql.service

Step 4:  Login as the postgreSQL user.

sudo su - postgre

Check clusters {notice the –check argument, this will not change any data before actual migration}.

/usr/lib/postgresql/15/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/14/main \
--new-datadir=/var/lib/postgresql/15/main \
--old-bindir=/usr/lib/postgresql/14/bin \
--new-bindir=/usr/lib/postgresql/15/bin \
--old-options '-c config_file=/etc/postgresql/14/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/15/main/postgresql.conf' \
--check

Migrate the data {without the –check argument}.

/usr/lib/postgresql/15/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/14/main \
--new-datadir=/var/lib/postgresql/15/main \
--old-bindir=/usr/lib/postgresql/14/bin \
--new-bindir=/usr/lib/postgresql/15/bin \
--old-options '-c config_file=/etc/postgresql/14/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/15/main/postgresql.conf'

Go back to the regular user.

exit

Swap the ports for the old (14) and new (15) PostgreSQL versions.

sudo vim /etc/postgresql/15/main/postgresql.conf
# ...and change "port = 5433" to "port = 5432"

sudo vim /etc/postgresql/14/main/postgresql.conf
# ...and change "port = 5432" to "port = 5433

Start the PostgreSQL service.

sudo systemctl start postgresql.service

Login as the PostgreSQL user again.

sudo su - postgres

Check the new PostgreSQL version.

psql -c "SELECT version();"

Run the recommended vacuumdb command:

/usr/lib/postgresql/15/bin/vacuumdb --all --analyze-in-stages

Go back to the regular user.

exit

Check which old PostgreSQL packages are installed.

apt list --installed | grep postgresql

Remove the old PostgreSQL packages (from the listing above).

sudo apt-get remove postgresql-14 postgresql-server-dev-14

Remove the old configuration.

sudo rm -rf /etc/postgresql/14/

Login as the postgres user once more.

sudo su - postgres

Finally, drop the old cluster data.

./delete_old_cluster.sh

Done!

Now, create an update script file using Vim to optimize and update Nextcloud system:

vim update.sh

Copy and paste below information into the new update.sh file:

# Ahuitech (October 26, 2022)
# Update script for Nextcloud 25.0.0.18 on ubuntu 22.04 system
#!/bin/bash
/usr/sbin/service nginx stop
sudo -u www-data php /var/www/nextcloud/updater/updater.phar
sudo -u www-data php /var/www/nextcloud/occ status
sudo -u www-data php /var/www/nextcloud/occ -V
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-primary-keys
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-indices
sudo -u www-data php /var/www/nextcloud/occ db:add-missing-columns
sudo -u www-data php /var/www/nextcloud/occ db:convert-filecache-bigint
sed -i "s/output_buffering=.*/output_buffering=0/" /var/www/nextcloud/.user.ini
chown -R www-data:www-data /var/nc_data /var/www
redis-cli -s /var/run/redis/redis-server.sock <<EOF
FLUSHALL
quit
EOF
sudo -u www-data php /var/www/nextcloud/occ user:list
sudo -u www-data php /var/www/nextcloud/occ files:scan --all
sudo -u www-data php /var/www/nextcloud/occ files:scan-app-data
sudo -u www-data php /var/www/nextcloud/occ app:update --all
/usr/sbin/service php8.1-fpm restart
/usr/sbin/service nginx restart
exit 0

Save the update.sh file in Vim using the following commands:

Press the Esc key first and then type :wq and then press Enter key.

Now, execute the update.sh file with the following commands:

chmod +x update.sh

./update.sh

chmod -x update.sh

Congratulations, you now have your own personal data on your own secured Nextcloud system!

At Ahuitech, we create clouds systems that allow you to have complete control of your Data.

Ahuitech – Cloud Creations are always in your Control

Leave a Reply

Your email address will not be published. Required fields are marked *