No New Messages
author avatar
Syed Reza
1465166598016 | NOTE

Systemctl Start Postgres Issue 001

These notes are mostly for myself but they may be of some use to others. If you're on Arch Linux and have either done a fresh install or an update of Postgres you may be seeing this error on start:

FATAL:  could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory

Navigate to /run and you'll notice that sure enough there is no postgresql folder.

You'll come across some folks saying the following: "just make the directory and chown it to user postgres". And they would be right (partially), make the directory chown it, and restart the postgres service and all will be peachy. But, restart your system and naturally the problem persists.

To fix the thing for good you'll want to edit your postgresql.service file, for most people that will be located at: /usr/lib/systemd/system/postgresql.service. It should look something like this:

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking
TimeoutSec=120
User=postgres
Group=postgres

Environment=PGROOT=/var/lib/postgres

SyslogIdentifier=postgres
PIDFile=/var/lib/postgres/data/postmaster.pid

PermissionsStartOnly=true
ExecStartPre=-/usr/bin/mkdir -p /run/postgresql
ExecStartPre=/usr/bin/chown -R postgres:postgres /run/postgresql
ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data
ExecStart=/usr/bin/pg_ctl -s -D ${PGROOT}/data start -w -t 120
ExecReload=/usr/bin/pg_ctl -s -D ${PGROOT}/data reload
ExecStop=/usr/bin/pg_ctl -s -D ${PGROOT}/data stop -m fast

# Due to PostgreSQL's use of shared memory, OOM killer is often overzealous in
# killing Postgres, so adjust it downward
OOMScoreAdjust=-200

# Additional security-related features
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Notice these extra lines:

PermissionsStartOnly=true
ExecStartPre=-/usr/bin/mkdir -p /run/postgresql
ExecStartPre=/usr/bin/chown -R postgres:postgres /run/postgresql

And that's it. The PermissionsStartOnly in the sample above, tells systemd to run everything before ExecStart as root, which is needed in order to create the directory. The minus sign after the equal tells systemd to ignore errors coming from that command, expected when the directory already exists.