Backup databases.
Check Debian version (v.10 - buster):
cat /etc/os-release
List of postgresql clusters:
pg_lsclusters
sudo apt update
sudo apt install -y curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Here we got an error on next step, because postgresql for Debian 10 was deleted from official repository. So add the string "deb https://apt-archive.postgresql.org/pub/repos/apt buster-pgdg main" to /etc/apt/sources.list.d/pgdg.list.
Install PostgreSQL 16:
sudo apt update
sudo apt install postgresql
If new cluster is not started: "sudo pg_createcluster 17 main --start" or "sudo -u postgres /usr/lib/postgresql/17/bin/initdb -D /var/lib/postgresql/17/main".
Stop all clusters.
cd /var/lib/postgresql/16/main
sudo -u postgres /usr/lib/postgresql/16/bin/pg_upgrade --check --old-datadir=/var/lib/postgresql/13/main --new-datadir=/var/lib/postgresql/16/main --old-bindir=/usr/lib/postgresql/13/bin --new-bindir=/usr/lib/postgresql/16/bin --old-options='-c config_file=/etc/postgresql/13/main/postgresql.conf' --new-options='-c config_file=/etc/postgresql/16/main/postgresql.conf'
If check okay, run migration:
sudo -u postgres /usr/lib/postgresql/16/bin/pg_upgrade --old-datadir=/var/lib/postgresql/13/main --new-datadir=/var/lib/postgresql/16/main --old-bindir=/usr/lib/postgresql/13/bin --new-bindir=/usr/lib/postgresql/16/bin --old-options='-c config_file=/etc/postgresql/13/main/postgresql.conf' --new-options='-c config_file=/etc/postgresql/16/main/postgresql.conf'
Edit new config, move settings from old config. Check files: postgresql.conf and pg_hba.conf.
sudo service postgresql restart
sudo -u postgres psql
SELECT version();
Delete old cluster:
sudo apt purge postgresql-13