PostgreSQL® Native Server SSL setup
This tutorial describes how to set up a PostgreSQL® native server on a Raspberry Pi. It is not exhaustive, for a production-ready setup follow the PostgreSQL Trust Authentication documentation.
Setup PostgreSQL® native server
Switch to the postgres
user, enter the postgres CLI, and change the postgres
DB default password:
Setup SSL server certificates
For SSL to work with PostgreSQL we need to generate three certificate files:
server.key
: the private key fileserver.crt
: the server certificate fileroot.crt
: the trusted root certificate
Find the server's data directory with:
From the data directory, we can now generate the key pair that will be used by the postgres server. Move to the data directory and create the request configuration file named server.cnf
following this template:
[req]
default_bits = 4096
default_md = sha256
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
C = IT
ST = FVG
L = Amaro
O = MyCompany
OU = MyDivision
CN = <ip-address>
[v3_req]
keyUsage = keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
IP.1 = <ip-address>
Tip
The example configuration file allows setting the IP address of the server running postgres as the IP SAN (Subject Alternative Names). For demo purposes, this allows to test the "Verify hostname" functionality.
The certificate and private key can be generated using the configuration file with the following command:
It is possible to inspect the generated certificate:
Allow read access and assing ownership of the private key to the postgres
user:
Since we are using a self-signed certificate, we will use it as the trusted root certificate by making a copy of the server certificate:
The server.crt
can be imported into Kura/ESF as a trusted certificate.
Setup PostgreSQL® server to use SSL
Edit the postgres configuration file. It can be retrieved with:
Edit the Connection Settings section as follows:
Edit the SSL section as follows:
ssl = on
ssl_ca_file = 'root.ca'
ssl_cert_file = 'server.crt'
ssl_crl_file = ''
ssl_key_file = 'server.key'
ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
ssl_prefer_server_ciphers = on
It is possible to configure the host based authentication by editing the pg_hba.conf
file. It can be retrieved with:
Edit such file to allow connections from all hosts under the IPv4 local connections section:
Restart the database:
Check that the server is accepting SSL connections: