PostgreSQL will not start: permission denied on the SSL private key
FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission deniedShort 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.
Fix it fast
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-readable2Match what you see, then jump to its fix
- 1
ls 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
The key is readable by group or others and the server rejects itThe key's permissions are more open than the server allowsFix
- 1
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 postgres2Fix
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 postgresql3Verify
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 postgresqlConfirmed by postgresql.org
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.key2Fix
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.key3Verify
ls shows -rw------- for the key and the server starts.
ls -l /path/to/server.key
sudo systemctl restart postgresqlConfirmed by postgresql.org
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
Servers
nginx cannot read the Let's Encrypt private key: permission denied
nginx fails to start or reload and names /etc/letsencrypt/live/example.com/privkey.pem
Certbot creates the live and archive folders under /etc/letsencrypt so only root can read them.
2 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fix