PostgreSQL Encrypted Communication

Ronald Farrer
3 min readMar 13, 2023
Photo by Jordan Harrison on Unsplash

PostgreSQL is an open-source relational database management system that supports various encryption options to secure data stored in the database. By default, PostgreSQL uses a data encryption mechanism called SSL/TLS to encrypt network communication between the client and the server. However, to further secure the data stored in the database, you can fine-tune the encryption settings and use secure ciphers. Here is a step-by-step guide on how to fine-tune PostgreSQL encryption settings and use secure ciphers:

Step 1: Enable SSL/TLS encryption

Before you can fine-tune PostgreSQL encryption settings and use secure ciphers, you need to enable SSL/TLS encryption. To do this, you need to modify the PostgreSQL configuration file (postgresql.conf) to specify the SSL/TLS settings.

  1. Open the postgresql.conf file using your preferred text editor. The location of this file depends on your installation. In a typical installation on Linux, the file is located in the /etc/postgresql/<version>/main directory.
  2. Search for the following lines in the file:
#ssl = off
#ssl_cert_file = 'server.crt'
#ssl_key_file = 'server.key'
  1. Uncomment these lines by removing the ‘#’ symbol at the beginning of each line.
  2. Specify the location of your SSL/TLS certificate and key files by modifying the ssl_cert_file and ssl_key_file parameters. These files should be stored in a secure location accessible only by the PostgreSQL server. For example:
ssl_cert_file = '/path/to/server.crt'
ssl_key_file = '/path/to/server.key'
  1. Save the postgresql.conf file and restart the PostgreSQL server to apply the changes.

Step 2: Configure SSL/TLS encryption options and use secure ciphers

Once SSL/TLS encryption is enabled, you can fine-tune the encryption settings and use secure ciphers by modifying the pg_hba.conf file. This file specifies how clients can connect to the PostgreSQL server and can be found in the same directory as the postgresql.conf file.

  1. Open the pg_hba.conf file using your preferred text editor.
  2. Locate the line that starts with “hostssl” and specify the SSL/TLS encryption options and use secure ciphers. This line specifies how remote clients can connect to the PostgreSQL server using SSL/TLS encryption. For example:
hostssl  all  all  0.0.0.0/0  md5  clientcert=1  sslmode=require ssl_ciphers=ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305

The above line allows any remote client to connect to the PostgreSQL server using SSL/TLS encryption with the “require” SSL mode. It also requires the client to present a valid SSL/TLS certificate (clientcert=1) and use the “md5” authentication method. Additionally, the line specifies the use of secure ciphers, such as ECDHE-ECDSA-AES256-GCM-SHA384, ECDHE-RSA-AES256-GCM-SHA384, ECDHE-ECDSA-CHACHA20-POLY1305, and ECDHE-RSA-CHACHA20-POLY1305.

  1. Save the pg_hba.conf file and restart the PostgreSQL server to apply the changes.

Step 3: Verify SSL/TLS encryption and secure ciphers

To verify that SSL/TLS encryption and secure ciphers are working correctly, you can use the following steps:

  1. Connect to the PostgreSQL server using thepsql command-line tool:
psql -h <hostname> -p <port> -U <username> -d <database> sslmode=require

Replace <hostname>, <port>, <username>, and <database> with the appropriate values for your setup.

  1. Once connected, run the following command to verify that SSL/TLS encryption and secure ciphers are being used:
SELECT ssl_is_used();

If the result is “t”, SSL/TLS encryption and secure ciphers are being used.

Conclusion:

In this article, we have shown you how to fine-tune PostgreSQL encryption settings to secure your data stored in the database and use secure ciphers. By enabling SSL/TLS encryption and configuring the appropriate SSL/TLS options with secure ciphers, you can ensure that your data is encrypted both in transit and at rest with the highest level of security.

I Love Coffee! https://ko-fi.com/canutethegreat

--

--