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 at build time (see Chapter 1).
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 server will look 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 server will prompt for the passphrase and
will not start until it has been entered.
The server will listen 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 Chapter 4 about 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 server's identity. To create a quick self-signed certificate, use the following OpenSSL command:
openssl req -new -text -out cert.req
Fill out the information that openssl asks for. Make sure that you enter the local host name as Common Name; the challenge password can be left blank. The script will generate a key that is passphrase protected; it will not accept a pass phrase that is less than four characters long. To remove the passphrase (as you must if you want automatic start-up of the server), run the commands
openssl rsa -in privkey.pem -out cert.pem
Enter the old passphrase to unlock the existing key. Now do
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
to turn the certificate into a self-signed certificate and to copy the key and certificate to where the server will look for them.