Skip to Content

2 ways to fix could not load server certificate file ‘server.crt’: No such file or directory in PostgreSQL

The error “could not load server certificate file ‘server.crt’: No such file or directory” in PostgreSQL indicates that the server is unable to find the server.crt file, which is required for SSL connections.

If you don’t need that, set “ssl = off”, then PostgreSQL won’t complain.

If you want transport encryption with TLS, you have to provide a server key and certificate.

In this article, we will dive into this issue.

Disable SSL in Postgres server

If you do not require SSL for your PostgreSQL server, you can disable it by setting the parameter “ssl” to “off” in your PostgreSQL configuration file.

Here are the steps to do it:

1.Open your PostgreSQL configuration file (postgresql.conf) using a text editor.

2.Find the line that specifies “ssl” and change the value to “off”.

3. Save the changes and close the file.

4. Restart your PostgreSQL server or run SELECT pg_reload_conf(); for the changes to take effect

Once you have disabled SSL, you should be able to start your PostgreSQL server without using SSL.

However, keep in mind that SSL provides an extra layer of security for your database connections, and disabling it may expose your data to potential security risks.

Enable SSL in Postgres server

To fix this error, you need to ensure that the server.crt file exists in the correct location and that PostgreSQL has permission to access it.

Here are the steps you can follow:

1.Check if the server.crt file exists in the expected directory. The default location is the data directory of your PostgreSQL installation. You can use the following command to find the data directory:

psql -c "SHOW data_directory"

2. If the server.crt file is not in the expected location, you need to find it and move it to the correct directory.

If you have a backup of the file, you can restore it from the backup. Otherwise, you may need to generate a new certificate file.

3. If the server.crt file exists in the correct location, you need to ensure that the PostgreSQL server has permission to access it.

You can do this by changing the ownership of the file to the PostgreSQL user and giving it the necessary permissions. You can use the following commands to change the ownership and permissions:

chown postgres:postgres server.crt
chmod 600 server.crt

4. Restart the PostgreSQL server or run SELECT pg_reload_conf(); to apply the changes. You can use the following command to restart the server:

systemctl restart postgresql

or

SELECT pg_reload_conf();

After following these steps, the error “could not load server certificate file ‘server.crt’: No such file or directory” should be resolved, and PostgreSQL should be able to use SSL connections.