## 16.7. Secure TCP/IP Connections with SSL

PostgreSQL has native support for using SSL connections to encrypt client/server communications for increased security. This requires that OpenSSL is installed on both client and server systems and that support in PostgreSQL is enabled at build time (see Chapter 14, Installation Instructions).

With SSL support compiled in, the PostgreSQL server can be started with SSL enabled by setting the parameter ssl to `on` in `postgresql.conf`. When starting in SSL mode, the server will look for the files `server.key` and `server.crt` in the data directory, which must 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 port, and will negotiate with any connecting client on whether to use SSL. By default, this is at the client's option; see Section 20.1, “The `pg_hba.conf` file” about how to set up the server to require use of SSL for some or all connections.

For details on how to create your server private key and certificate, refer to the OpenSSL documentation. A self-signed certificate can be used for testing, but a certificate signed by a certificate authority (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 server.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 program will generate a key that is passphrase protected; it will not accept a passphrase 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 server.key
    rm privkey.pem

Enter the old passphrase to unlock the existing key. Now do

    openssl req -x509 -in server.req -text -key server.key -out server.crt
    chmod og-rwx server.key

to turn the certificate into a self-signed certificate and to copy the key and certificate to where the server will look for them.

If verification of client certificates is required, place the certificates of the CA(s) you wish to check for in the file `root.crt` in the data directory. When present, a client certificate will be requested from the client during SSL connection startup, and it must have been signed by one of the certificates present in `root.crt`.

When the `root.crt` file is not present, client certificates will not be requested or checked. In this mode, SSL provides communication security but not authentication.

The files `server.key`, `server.crt`, and `root.crt` are only examined during server start; so you must restart the server to make changes in them take effect.