PostgreSQL Failing to Start Even after Reinstalling on Arch Linux
It’s been some months since I’ve last used PostgreSQL on my Arch Linux desktop, and, apparently, after some updates, something must have broken. If I ask for the service’s status, I get (systemctl status postgresql
):
× postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: disabled)
Active: failed (Result: exit-code) since Wed 2024-02-21 16:00:22 -03; 1h 28min ago
CPU: 54ms
fev 21 16:00:22 phili systemd[1]: Starting PostgreSQL database server...
fev 21 16:00:22 phili postgres[11667]: An old version of the database format was found.
fev 21 16:00:22 phili postgres[11667]: See https://wiki.archlinux.org/index.php/PostgreSQL#>
fev 21 16:00:22 phili systemd[1]: postgresql.service: Control process exited, code=exited, >
fev 21 16:00:22 phili systemd[1]: postgresql.service: Failed with result 'exit-code'.
fev 21 16:00:22 phili systemd[1]: Failed to start PostgreSQL database server.
I’ve already tried to reinstall in various ways. But mostly with something like this:
pacman -Rsn postgresql
rm -rf /var/lib/postgres
pacman -Syu postgresql
The version I’m installing is the latest one, 16, but I’ve already tried all this with 14 as well. And, yes, I have tried restarting my PC several times.
When you initialize a PostgreSQL database, the database data is keyed to to that major version of PostgreSQL.
When you upgrade the major version of PostgreSQL, you need to make sure the database data is compatible with that new version.
Arch Linux provides a wiki entry for exactly this scenario, the steps are summarized as follows:
- Make a backup of your database data
- Try to retrieve the
initdb
arguments used to create the initial database - Stop the
postgresql.service
- Upgrade all the PostgreSQL packages
- Delete any existing old data from previous database upgrades
- Move your database data to a temporary location and make a new directory to store the upgraded database.
- Initialize a new database with the same
initdb
arguments as the original database. - Upgrade your previous database with
pg_upgrade
- Start the
postgresql.service
Sans any errors, your PostgreSQL database server should now be running with upgraded database. You can now clean up any old data and temporary directories if you wish.
Following some of @GracefulRestart’s comments, what works in the end for me was following some of the steps in the 8.1 pg_upgrade
section:
- Installing
postgresql-old-upgrade
- Rename the old cluster directory, then create a new cluster and temporary working directory:
mv /var/lib/postgres/data /var/lib/postgres/olddata mkdir /var/lib/postgres/data /var/lib/postgres/tmp chown postgres:postgres /var/lib/postgres/data /var/lib/postgres/tmp
- Initialize the new cluster using the same initdb arguments as were used for the old cluster:
[postgres]$ initdb -D /var/lib/postgres/data --locale=C.UTF-8 --encoding=UTF8 --data-checksums