PostgreSQL does not start

After the UPdate, there is an error message about postgresql during boot (could not read it completely due to time) and after log-in, it’s impossible to start the connection:

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Postgresql 15.2.1 is installed, I used version 15 before.

$ 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 Mon 2023-04-03 12:16:27 CEST; 31min ago
    Process: 834 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=1/FAILURE)
        CPU: 30ms

Apr 03 12:16:27 machine systemd[1]: Starting PostgreSQL database server...
Apr 03 12:16:27 machine postgres[834]: An old version of the database format was found.
Apr 03 12:16:27 machine postgres[834]: See https://wiki.archlinux.org/index.php/PostgreSQL#Upgrading_PostgreSQL
Apr 03 12:16:27 machine systemd[1]: postgresql.service: Control process exited, code=exited, status=1/FAILURE
Apr 03 12:16:27 machine systemd[1]: postgresql.service: Failed with result 'exit-code'.
Apr 03 12:16:27 machine systemd[1]: Failed to start PostgreSQL database server.

How to solve this?

You need to migrate the data from the old version to the currently installed one.

Read the arch wiki on postgres on the how to.

Thank you, but this is not the issue; postgresql15 has been running already before the update …

Your log begs to differ.

We found the problem: In order to use an user-defined data-directory, we modified /usr/lib/systemd/system/postgresql.service having now one line more:

#Environment=PGROOT=/var/lib/postgres
Environment=PGROOT=/mnt/myUserDefinedDirectory/PostgreSQL

Now the UPdate delivered the former version of /usr/lib/systemd/system/postgresql.service.
What do you recommend to go around it?

Never edit files installed by a package manager. They get overridden with every update. Only few files in /etc/ will get a pacnew ending.

Either create an override file, ( sudo systemctl edit postgresql.service ) or copy the complete file to /etc/systemd/system and make your necessary changes. After copy and edit do a systemctl daemon-reload .
An alternative would be to do sudo systemctl edit -all postgresql.service (only one dash in front of all) , make the changes and save. Wich is basically the same as to copy the file in /etc/systemd/system and make changes.

3 Likes

Thank you, this looks good:

Just for my understanding (Sorry, never heard about that way before): The command sudo systemctl edit postgresql.service copies the file, each line marked as comment with initial #? Now I edit the change uncomment that line and save the file. And the system will care about the override by its own?

Just edit (without -all) will not copy anything. The comments are just for you and will not be in the override file. Don’t forget to also specify the correct section. This needs to be in override file as well.

The created override file will be shown in the status output as used in combination with the system service file.

Thank you:

Sorry, I don’t understand. This is what I did:

$ cd /usr/lib/systemd/system/
sudo systemctl edit postgresql.service

Now I’m in nano, having the original file, each line starting with #
What are my next steps?

Read the comments. It explains where you can add own additions that are then part of the “drop-in” or override file.

You would need to add

[Service]
Environment=PGROOT=/mnt/myUserDefinedDirectory/PostgreSQL

Then save the file.
You can do a

systemctl status postgresql.service

The Drop-In should be listed.

1 Like

Am I allowed to delete all lines in the nano file (as it are all comments), add

[Service]
Environment=PGROOT=/mnt/myUserDefinedDirectory/PostgreSQL

and save the file?

Yes, but the comments will not be in the override file. If you save it and then rerun the edit command, the comments will be shown again.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.