4.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.
Conditions préalables
- The PostgreSQL server is installed.
- The database cluster is initialized.
Procédure
Install the OpenSSL library:
dnf install openssl
# dnf install openssl
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a TLS certificate and a key:
openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=dbhost.yourdomain.com"
# openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=dbhost.yourdomain.com"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace dbhost.yourdomain.com with your database host and domain name.
Copy your signed certificate and your private key to the required locations on the database server:
cp server.{key,crt} /var/lib/pgsql/data/.
# cp server.{key,crt} /var/lib/pgsql/data/.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the owner and group ownership of the signed certificate and your private key to the
postgres
user:chown postgres:postgres /var/lib/pgsql/data/server.{key,crt}
# chown postgres:postgres /var/lib/pgsql/data/server.{key,crt}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restrict the permissions for your private key so that it is readable only by the owner:
chmod 0400 /var/lib/pgsql/data/server.key
# chmod 0400 /var/lib/pgsql/data/server.key
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the password hashing algorithm to
scram-sha-256
by changing the following line in the/var/lib/pgsql/data/postgresql.conf
file:#password_encryption = md5 # md5 or scram-sha-256
#password_encryption = md5 # md5 or scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
password_encryption = scram-sha-256
password_encryption = scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure PostgreSQL to use SSL/TLS by changing the following line in the
/var/lib/pgsql/data/postgresql.conf
file:#ssl = off
#ssl = off
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
ssl=on
ssl=on
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restrict access to all databases to accept only connections from clients using TLS by changing the following line for the IPv4 local connections in the
/var/lib/pgsql/data/pg_hba.conf
file:host all all 127.0.0.1/32 ident
host all all 127.0.0.1/32 ident
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
hostssl all all 127.0.0.1/32 scram-sha-256
hostssl all all 127.0.0.1/32 scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can restrict access for a single database and a user by adding the following new line:
hostssl mydatabase mydbuser 127.0.0.1/32 scram-sha-256
hostssl mydatabase mydbuser 127.0.0.1/32 scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace mydatabase with the database name and mydbuser with the username.
Make the changes effective by restarting the
postgresql
service:systemctl restart postgresql.service
# systemctl restart postgresql.service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Vérification
To manually verify that the connection is encrypted:
Connect to the PostgreSQL database as the mydbuser user, specify the host name and the database name:
psql -U mydbuser -h 127.0.0.1 -d mydatabase
$ psql -U mydbuser -h 127.0.0.1 -d mydatabase Password for user mydbuser:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace mydatabase with the database name and mydbuser with the username.
Obtain information about the current database connection:
mydbuser=> \conninfo You are connected to database "mydatabase" as user "mydbuser" on host "127.0.0.1" at port "5432". SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
mydbuser=> \conninfo You are connected to database "mydatabase" as user "mydbuser" on host "127.0.0.1" at port "5432". SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can write a simple application that verifies whether a connection to PostgreSQL is encrypted. This example demonstrates such an application written in C that uses the
libpq
client library, which is provided by thelibpq-devel
package:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace mypassword with the password, mydatabase with the database name, and mydbuser with the username.
NoteYou must load the
pq
libraries for compilation by using the-lpq
option. For example, to compile the application by using the GCC compiler:gcc source_file.c -lpq -o myapplication
$ gcc source_file.c -lpq -o myapplication
Copy to Clipboard Copied! Toggle word wrap Toggle overflow where the source_file.c contains the example code above, and myapplication is the name of your application for verifying secured PostgreSQL connection.
Exemple 4.4. Initializing, creating, and connecting to a PostgreSQL database using TLS encryption
This example demonstrates how to initialize a PostgreSQL database, create a database user and a database, and how to connect to the database using a secured connection.
Install the PosgreSQL server:
dnf install postgresql-server
# dnf install postgresql-server
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Initialize the database cluster:
postgresql-setup --initdb
# postgresql-setup --initdb * Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Install the OpenSSL library:
dnf install openssl
# dnf install openssl
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a TLS certificate and a key:
openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=dbhost.yourdomain.com"
# openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=dbhost.yourdomain.com"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace dbhost.yourdomain.com with your database host and domain name.
Copy your signed certificate and your private key to the required locations on the database server:
cp server.{key,crt} /var/lib/pgsql/data/.
# cp server.{key,crt} /var/lib/pgsql/data/.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the owner and group ownership of the signed certificate and your private key to the
postgres
user:chown postgres:postgres /var/lib/pgsql/data/server.{key,crt}
# chown postgres:postgres /var/lib/pgsql/data/server.{key,crt}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restrict the permissions for your private key so that it is readable only by the owner:
chmod 0400 /var/lib/pgsql/data/server.key
# chmod 0400 /var/lib/pgsql/data/server.key
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the password hashing algorithm to
scram-sha-256
. In the/var/lib/pgsql/data/postgresql.conf
file, change the following line:#password_encryption = md5 # md5 or scram-sha-256
#password_encryption = md5 # md5 or scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
password_encryption = scram-sha-256
password_encryption = scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure PostgreSQL to use SSL/TLS. In the
/var/lib/pgsql/data/postgresql.conf
file, change the following line:#ssl = off
#ssl = off
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
ssl=on
ssl=on
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
postgresql
service:systemctl start postgresql.service
# systemctl start postgresql.service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log in as the system user named
postgres
:su - postgres
# su - postgres
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the PostgreSQL interactive terminal as the
postgres
user:psql -U postgres
$ psql -U postgres psql (13.7) Type "help" for help. postgres=#
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a user named
mydbuser
and set a password formydbuser
:postgres=# CREATE USER mydbuser WITH PASSWORD 'mypasswd'; CREATE ROLE postgres=#
postgres=# CREATE USER mydbuser WITH PASSWORD 'mypasswd'; CREATE ROLE postgres=#
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a database named
mydatabase
:postgres=# CREATE DATABASE mydatabase; CREATE DATABASE postgres=#
postgres=# CREATE DATABASE mydatabase; CREATE DATABASE postgres=#
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Grant all permissions to the
mydbuser
user:postgres=# GRANT ALL PRIVILEGES ON DATABASE mydatabase TO mydbuser; GRANT postgres=#
postgres=# GRANT ALL PRIVILEGES ON DATABASE mydatabase TO mydbuser; GRANT postgres=#
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log out of the interactive terminal:
postgres=# \q
postgres=# \q
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log out of the
postgres
user session:logout
$ logout
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restrict access to all databases to accept only connections from clients using TLS by changing the following line for the IPv4 local connections in the
/var/lib/pgsql/data/pg_hba.conf
file:host all all 127.0.0.1/32 ident
host all all 127.0.0.1/32 ident
Copy to Clipboard Copied! Toggle word wrap Toggle overflow à :
hostssl all all 127.0.0.1/32 scram-sha-256
hostssl all all 127.0.0.1/32 scram-sha-256
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Make the changes effective by restarting the
postgresql
service:systemctl restart postgresql.service
# systemctl restart postgresql.service
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Connect to the PostgreSQL database as the
mydbuser
user, specify the host name and the database name:Copy to Clipboard Copied! Toggle word wrap Toggle overflow