3.5. Configuring TLS encryption on a PostgreSQL server
By default, PostgreSQL uses unencrypted connections. For more secure connections, you can enable Transport Layer Security (TLS) support on the PostgreSQL server and configure your clients to establish encrypted connections.
Prerequisites
- You created a TLS private key and a certificate authority (CA) issued a server certificate for your PostgreSQL server.
- The PostgreSQL server is installed.
- The database cluster is initialized.
- If FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced on RHEL 9.2 and later.
Procedure
Store the private key and the server certificate in the
/var/lib/pgsql/data/directory:# cp server.{key,crt} /var/lib/pgsql/data/Set the ownership of the private key and certificate:
# chown postgres:postgres /var/lib/pgsql/data/server.{key,crt}Set permissions on the server certificate that enable only the PostgreSQL server to read the file:
# chmod 0400 /var/lib/pgsql/data/server.keyBecause certificates are part of the communication before a secure connection is established, any client can retrieve them without authentication. Therefore, you do not need to set strict permissions on the server certificate file.
Edit the
/var/lib/pgsql/data/postgresql.conffile and make the following changes:Set the
scram-sha-256hashing algorithm:password_encryption = scram-sha-256Enable TLS encryption:
ssl = on
Edit the
/var/lib/pgsql/data/pg_hba.conffile and update the authentication entries to use TLS encryption and thescram-sha-256hashing algorithm. For example, changehostentries tohostsslto enable TLS encryption, and set thescram-sha-256hashing algorithm in the last column:hostssl all all 192.0.2.0/24 scram-sha-256Restart the
postgresqlservice:# systemctl restart postgresql.service
Verification
Use the
postgressuper user to connect to a PostgreSQL server and execute the\conninfometa command:# psql "postgresql://postgres@localhost:5432" -c '\conninfo' Password for user postgres: You are connected to database "postgres" as user "postgres" on host "192.0.2.1" at port "5432". SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)