Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

PostgreSQL will not start because it may not create its lock file

Your error

FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": Permission denied1 more line it can show

Fix it fast

Most common cause, 1 of 3

The server is started by an account that cannot write the socket directory

Start the server as the account that owns the data directory and the socket directory.

Fix
# system cluster: run as its owner
sudo -u postgres pg_ctl -D /path/to/data start

# personal cluster: socket in /tmp instead
pg_ctl -D ~/pgdata -o "-k /tmp" start
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

FATAL: could not create lock file ... Permission denied means the account starting PostgreSQL cannot write to the directory where the lock belongs. For .s.PGSQL.5432.lock that is the Unix socket directory; for postmaster.pid it is the data directory. Start the server as the account that owns them, point unix_socket_directories elsewhere, or restore write access. PostgreSQL 15 to 19.

  • Applies to PostgreSQL 15 to 19 (19.0, 18.7, 17.12, 16.16, 15.20), Linux and macOS servers started with pg_ctl, postgres or a service manager
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The quoted path says which directory is refusing the write: a path ending in .s.PGSQL.<port>.lock lives in a socket directory, postmaster.pid lives in the data directory. Compare the account you start the server with against the owner and mode of that directory.

Run this first
whoami
ls -ld /var/run/postgresql          # directory from the .s.PGSQL lock path
ls -ld /path/to/data                # data directory, for postmaster.pid

Then find the line you see in this list and open its fix:

  1. 1The server is started by an account that cannot write the socket directoryThe path ends in .s.PGSQL.5432.lock, and ls -ld shows the directory owned by another account (for example postgres) while whoami prints your own login
  2. 2The socket directory itself has the wrong owner or modeYou do start as the right account, but ls -ld shows the socket directory owned by root or without write permission for that account
  3. 3The data directory lost its owner write permissionThe message names postmaster.pid, and ls -ld on the data directory shows a mode like dr-x------ (no w for the owner)

What does the error look like?

  • FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": Permission denied
  • FATAL: could not create lock file "postmaster.pid": Permission denied

Cause 1 of 3

The server is started by an account that cannot write the socket directory

You seeThe path ends in .s.PGSQL.5432.lock, and ls -ld shows the directory owned by another account (for example postgres) while whoami prints your own login

Check

This lock-file error appears when you run postgres or pg_ctl by hand under a different account than the one the socket directory was made for. Besides the socket itself, the server writes a .s.PGSQL.<port>.lock file in every directory listed in unix_socket_directories, so it needs write access there before it accepts any connection. The PostgreSQL documentation expects the server to be started while logged into the PostgreSQL user account.

Check
ls -ld /var/run/postgresql
# which socket directories this cluster is configured for (postgres may live in the version's bin folder)
sudo -u postgres postgres -D /path/to/data -C unix_socket_directories

Fix

Start the server as the account that owns the data directory and the socket directory. For a personal test cluster in your home folder, keep your own account but move the socket to a directory you can write.

Fix
# system cluster: run as its owner
sudo -u postgres pg_ctl -D /path/to/data start

# personal cluster: socket in /tmp instead
pg_ctl -D ~/pgdata -o "-k /tmp" start

Verify

The server stays up and a client connects through the socket directory you chose.

Verify
pg_isready -h /tmp

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

The socket directory itself has the wrong owner or mode

You seeYou do start as the right account, but ls -ld shows the socket directory owned by root or without write permission for that account

Check

If even the server's own account gets Permission denied on the .lock path, the directory was recreated or changed with the wrong owner, for example by a manual mkdir as root or a recursive chown. The server cannot create its lock file there no matter how it is launched.

Check
ls -ld /var/run/postgresql
sudo -u postgres test -w /var/run/postgresql && echo writable || echo not-writable

Fix

Hand ownership of the socket directory to the PostgreSQL service account, confirm that account can write there, and start the cluster as usual.

Fix
sudo chown postgres:postgres /var/run/postgresql
sudo -u postgres test -w /var/run/postgresql && echo writable

Verify

The server starts and the lock file appears next to the socket, owned by the server's account.

Verify
ls -l /var/run/postgresql/.s.PGSQL.5432*

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

The data directory lost its owner write permission

You seeThe message names postmaster.pid, and ls -ld on the data directory shows a mode like dr-x------ (no w for the owner)

Check

A postmaster.pid Permission denied means the owner may enter the data directory but not create files in it. PostgreSQL's start-up checks only reject a wrong owner or too-open group and other bits, so a directory restored or copied with mode 0500 gets past them and fails at the lock file instead.

Check
stat -c '%U %a %n' /path/to/data   # GNU stat; on macOS: stat -f '%Su %Lp %N'

Fix

Restore the mode the server expects, 0700 (or 0750 for group read access), keeping the owner unchanged.

Fix
sudo chmod 700 /path/to/data

Verify

pg_ctl starts the server and postmaster.pid exists in the data directory.

Verify
sudo -u postgres pg_ctl -D /path/to/data start
ls -l /path/to/data/postmaster.pid

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Start and stop server clusters only through the service manager or as their owning account, never with your own login.
  • Give personal development clusters their own socket directory (-k or unix_socket_directories) so they never touch the system one.
  • After restoring or copying a data directory, check its owner and mode with stat before the first start.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.