SSL Connection to PostgreSQL
Establishing SSL Connection to PostgreSQL
Encrypt PostgreSQL connections to protect authentication credentials and database traffic from interception. This guide configures the PostgreSQL server for SSL, generates server and client certificates, and establishes a secure connection through pgAdmin.
PostgreSQL Server Configuration
Create an environment with a PostgreSQL database if one is not already available.

Connect to PostgreSQL through SSH
Connect to the PostgreSQL database server through the platform SSH Gate. If SSH access is not configured yet, generate an SSH key pair, add the public key to the dashboard, and then connect to the account through SSH.

SSL requires three files in the PostgreSQL data directory:
Generate Server Certificates
Create the Private Key
cd /var/lib/pgsql/data openssl genrsa -des3 -out server.key 1024
Enter and confirm a pass phrase when prompted.

Remove the pass phrase from the key so PostgreSQL can use it:
openssl rsa -in server.key -out server.key

Set the required permissions and ownership:
chmod 400 server.key chown postgres.postgres server.key

Create the Server Certificate
openssl req -new -key server.key -days 3650 -out server.crt -x509 \ -subj '/C=US/ST=California/L=PaloAlto/O=Jelastic/CN=mysite.com/emailAddress=mail@jelastic.com'
For production, replace the example subject values with the actual certificate identity information. The subject can also be entered interactively by omitting the -subj parameter.

Because the example uses a self-signed certificate, copy the server certificate as the trusted root certificate:
cp server.crt root.crt

Configure pg_hba.conf
Open /var/lib/pgsql/data/pg_hba.conf and configure local and SSL-authenticated connections:
# TYPE DATABASE USER CIDR-ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv4 remote connections for authenticated users hostssl all webadmin 0.0.0.0/0 md5 clientcert=verify-full
If a PostgreSQL user other than the default webadmin is used, replace the username in the last line and use the same username in the later certificate and connection steps.
PostgreSQL 10 and earlier
The source documentation uses md5 clientcert=1 instead of clientcert=verify-full for PostgreSQL 10 and older.
hostssl all webadmin 0.0.0.0/0 md5 clientcert=1

Enable SSL in postgresql.conf
Open postgresql.conf, locate the Security and Authentication section, enable SSL, and specify the root certificate:
ssl = on ssl_ca_file = 'root.crt'

Save the changes and restart PostgreSQL:
sudo service postgresql restart

Create Client Certificates
Reconnect to the PostgreSQL server through SSH and generate a private key for the client:
openssl genrsa -des3 -out /tmp/postgresql.key 1024 openssl rsa -in /tmp/postgresql.key -out /tmp/postgresql.key

Create a client certificate for the PostgreSQL user and sign it with root.crt and server.key:
openssl req -new -key /tmp/postgresql.key -out /tmp/postgresql.csr \ -subj '/C=US/ST=California/L=PaloAlto/O=Jelastic/CN=webadmin' openssl x509 -req -in /tmp/postgresql.csr -CA root.crt -CAkey server.key \ -out /tmp/postgresql.crt -CAcreateserial
Common Name must match the PostgreSQL user
The /CN= value in the client certificate must match the database username configured for SSL access, which is webadmin in the source example.

Move these three files to the .postgresql folder on the client machine:
postgresql.keypostgresql.crtroot.crt
If the directory does not exist, create it:
mkdir ~/.postgresql
Optionally, restrict client-key access:
chmod 0400 ~/.postgresql/postgresql.key

Clean up temporary files
Remove temporary client keys and certificate files from the database server after they have been transferred to the client.
Establish a Secure Connection through pgAdmin
A public IP address or endpoint must be attached to the PostgreSQL container. The source example uses an endpoint.
Open environment Settings, go to Endpoints, and add a new endpoint.

In pgAdmin 3, choose New Server Registration. In the Properties tab, specify:
ssl-to-pgsql.5432 for a public IP, or the endpoint’s Public port.webadmin by default.
Switch to the SSL tab and set SSL to require.

Click OK. pgAdmin automatically loads the certificate files during the first connection and opens a secure PostgreSQL session.
Expected Result
The PostgreSQL server accepts SSL-authenticated remote connections using the configured server and client certificates. The client connects through a public IP or endpoint, and pgAdmin establishes the database session with SSL set to require.
Important Notes
- The source tutorial uses self-signed certificates, but a custom SSL certificate can also be used.
- Keep
server.keyrestricted to the PostgreSQL owner with secure permissions. - The client certificate Common Name must match the PostgreSQL username.
- Use
clientcert=verify-fullfor the documented modern PostgreSQL configuration; the source usesclientcert=1for PostgreSQL 10 and older. - SSL must be enabled in
postgresql.confand PostgreSQL must be restarted afterward. - A public IP or endpoint is required for the remote pgAdmin example.
- Remove temporary certificate files from the database server after transferring them to the client.
