Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

PostgreSQL will not start: permission denied on the SSL private key

ErrorFATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied

Short answer

PostgreSQL only accepts a private key that nobody but the server can read. Either the key is mode 0600 and owned by the server user, or it is owned by root with mode 0640 and the server user belongs to the file's group. If the group setup is used, add the postgres user to that group and restart.

  • Applies to PostgreSQL 16, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at who owns the key and what mode it has, then ask whether the postgres user can read it. The output tells you which cause below applies.

    ls -l /etc/ssl/private/ssl-cert-snakeoil.key
    id postgres
    sudo -u postgres test -r /etc/ssl/private/ssl-cert-snakeoil.key && echo readable || echo not-readable
  2. 2Match what you see, then jump to its fix

    1. 1ls shows root as owner, a group such as ssl-cert, mode -rw-r-----, and id postgres does not list that groupThe key is group-readable but the postgres user is not in that groupFix
    2. 2The key is readable by group or others and the server rejects itThe key's permissions are more open than the server allowsFix

What does the error look like?

  • FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied

Cause 1 of 2

The key is group-readable but the postgres user is not in that group

1Check

The PostgreSQL manual allows a root-owned key with 0640 permissions for installs where the operating system manages certificate files. It then expects the server user to be a member of the group that can read them.

ls -l /etc/ssl/private/ssl-cert-snakeoil.key
id postgres

2Fix

Add the postgres user to the group shown by ls, then restart the service. Group membership is read when a process starts, so a reload is not enough.

sudo usermod -a -G ssl-cert postgres
sudo systemctl restart postgresql

3Verify

The postgres user can read the key and the server starts.

sudo -u postgres test -r /etc/ssl/private/ssl-cert-snakeoil.key && echo readable
sudo systemctl status postgresql

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 2

The key's permissions are more open than the server allows

1Check

The manual says that on Unix the permissions on server.key must disallow any access to world or group, for example with chmod 0600. The server rejects a file with looser permissions.

ls -l /path/to/server.key

2Fix

Tighten the mode on your own key file, and give the key to the user that runs the server. Then point ssl_key_file at it.

sudo chown postgres:postgres /path/to/server.key
sudo chmod 0600 /path/to/server.key

3Verify

ls shows -rw------- for the key and the server starts.

ls -l /path/to/server.key
sudo systemctl restart postgresql

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Give a custom key either mode 0600 for the server user, or root ownership with 0640 and a group the server user is in.
  • Remember that SSL files are read at server start and on reload, and that a bad file at start stops the server.
  • After you change group membership, restart the service instead of reloading it.

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?

Related fixes

Next step

Want this handled for good?

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