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 showFix it fast
Most common cause, 1 of 3The 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.
# 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" startShort 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.
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.
whoami
ls -ld /var/run/postgresql # directory from the .s.PGSQL lock path
ls -ld /path/to/data # data directory, for postmaster.pidThen find the line you see in this list and open its fix:
- 1The server is started by an account that cannot write the socket directory
The 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 - 2The socket directory itself has the wrong owner or mode
You do start as the right account, but ls -ld shows the socket directory owned by root or without write permission for that account - 3The data directory lost its owner write permission
The 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 deniedFATAL: 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.
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_directoriesFix
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.
# 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" startVerify
The server stays up and a client connects through the socket directory you chose.
pg_isready -h /tmpConfirmed by postgresql.org
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.
ls -ld /var/run/postgresql
sudo -u postgres test -w /var/run/postgresql && echo writable || echo not-writableFix
Hand ownership of the socket directory to the PostgreSQL service account, confirm that account can write there, and start the cluster as usual.
sudo chown postgres:postgres /var/run/postgresql
sudo -u postgres test -w /var/run/postgresql && echo writableVerify
The server starts and the lock file appears next to the socket, owned by the server's account.
ls -l /var/run/postgresql/.s.PGSQL.5432*Confirmed by postgresql.org
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.
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.
sudo chmod 700 /path/to/dataVerify
pg_ctl starts the server and postmaster.pid exists in the data directory.
sudo -u postgres pg_ctl -D /path/to/data start
ls -l /path/to/data/postmaster.pidConfirmed by raw.githubusercontent.com
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.