PostgreSQL has native support for connections over SSL to encrypt client/server communications for increased security. This requires OpenSSL to be installed on both client and server systems and support enabled.
With SSL support compiled in, the PostgreSQL server can be started with the argument -l (ell) to enable SSL connections. When starting in SSL mode, the postmaster looks for the files server.key and server.crt in the data directory. These files should contain the server private key and certificate respectively. These files must be set up correctly before an SSL-enabled server can start. If the private key is protected with a passphrase, the postmaster prompts for the passphrase and will not start until it has been entered.
The postmaster listens for both standard and SSL connections on the same TCP/IP port, and will negotiate with any connecting client whether or not to use SSL. See the chapter "Client Authentication" to learn how to force on the server side the use of SSL for certain connections.
For details on how to create your server private key and certificate, refer to the OpenSSL documentation. A simple self-signed certificate can be used to get started for testing, but a certificate signed by a CA (either one of the global CAs or a local one) should be used in production so the client can verify the servers identity. To create a quick self-signed certificate, use the following OpenSSL command:
openssl req -new -text -out cert.req |
openssl rsa -in privkey.pem -out cert.pem |
openssl req -x509 -in cert.req -text -key cert.pem -out cert.cert cp cert.pem $PGDATA/server.key cp cert.cert $PGDATA/server.crt |