Upgrade Postgresql From 14 to 15

Date: 2023/12/03
Last Updated: 2024-03-07T20:23:34.701Z
Categories: Postgresql, Database
Tags: Postgresql, Database
Read Time: 2 minutes

Recently, I have upgraded my Postgresql database for my freshrss instance from 14 to 15. Here are the steps I followed to do it.

First, we need to local the binary of the old version of Postgresql. In most of the cases, it can be found by

which psql

In my case, it is /usr/local/bin/psql.

Then, we need to find the data directory of the old version of Postgresql. In my case, it is at /var/lib/postgres/14. And the postgresql.conf file is at /var/lib/postgres/14/data/postgresql.conf.

Then, we need to install the new version of Postgresql. I use nix to manage my packages, so I just change the version of Postgresql in my configuration.nix file and run nixos-rebuild switch.

After that, we need to find the binary of the new version of Postgresql. Which can be found by

which psql

Then, we need to find the data directory of the new version of Postgresql. In my case, it is at /var/lib/postgres/15. And the postgresql.conf file is which you should copy and modify from the old version of Postgresql.

We need to first stop the old version of Postgresql.

systemctl stop postgresql

And su to the postgres user.

sudo su - postgres

Then, we need to run the following command to first initialize the new version of Postgresql.

initdb -D /var/lib/postgres/15/

Then, we need

Then, we need to run the following command to migrate the data from the old version of Postgresql to the new version of Postgresql. With --check option, it will check if the migration is possible.

/usr/lib/postgresql/15/bin/pg_upgrade \
  --old-datadir=$OLD_DATADIR \
  --new-datadir=$NEW_DATADIR \
  --old-bindir=$OLD_BINDOR \
  --new-bindir=$NEW_BINDIR \
  --old-options '-c config_file=$OLD_CONFIG_FILE' \
  --new-options '-c config_file=$NEW_CONFIG_FILE' \
  --check

If the check is successful, we can run the following command to do the migration.

/usr/lib/postgresql/15/bin/pg_upgrade \
  --old-datadir=$OLD_DATADIR \
  --new-datadir=$NEW_DATADIR \
  --old-bindir=$OLD_BINDOR \
  --new-bindir=$NEW_BINDIR \
  --old-options '-c config_file=$OLD_CONFIG_FILE' \
  --new-options '-c config_file=$NEW_CONFIG_FILE'

After that, we need to check if the migration is successful. If it is successful, we can then remove the old version of Postgresql.


我登上那高山 自由地飞翔在那辽阔蓝天

我站在云端 脚下是一望无际的海面

我不再犹豫 我心中有爱

我不想放弃 只想离开


我飞过那高山 眼前是阳光灿烂辽阔蓝天

我穿过云海 脚下是一片金色的麦田

我不再犹豫 我心中有爱

我不想放弃 只想离开


我没有选择 一篇空白

我不要选择 我想离开

我没有选择 坠落尘埃

我不要选择 我想要自由自在

我没有选择 徘徊期待

我想要自由自在 快离开