Update PostgreSQL version with pg_upgrade
Before running pg_upgrade:
- Both versions must be installed. New version’s cluster must be empty.
 
Power off both clusters:
_$: sudo systemctl stop postgresql@12-main.service
_$: sudo systemctl stop postgresql@14-main.service
Migrate your cluster (all databases) from version 12 to 14:
/usr/lib/postgresql/14/bin/pg_upgrade \
  --old-datadir /var/lib/postgresql/12/main \
  --new-datadir /var/lib/postgresql/14/main \
  --old-bindir /usr/lib/postgresql/12/bin \
  --new-bindir /usr/lib/postgresql/14/bin \
  --old-options ' -c config_file=/etc/postgresql/12/main/postgresql.conf' \
  --new-options ' -c config_file=/etc/postgresql/14/main/postgresql.conf'
Change the port to 5432 in the new cluster. Start the new cluster and check that everything is ok:
_$: sudo systemctl start postgresql@14-main.service
Delete the old cluster:
_$: sudo pg_dropcluster 12 main