18.9. Secure TCP/IP Connections with SSL#
PostgreSQL has native support for usingSSL connections to encrypt client/server communications for increased security. This requires thatOpenSSL is installed on both client and server systems and that support inPostgreSQL is enabled at build time (seeChapter 17).
The termsSSL andTLS are often used interchangeably to mean a secure encrypted connection using aTLS protocol.SSL protocols are the precursors toTLS protocols, and the termSSL is still used for encrypted connections even thoughSSL protocols are no longer supported.SSL is used interchangeably withTLS inPostgreSQL. WithSSL support compiled in, thePostgreSQL server can be started with support for encrypted connections usingTLS protocols enabled by setting the parameterssl to To start inSSL mode, files containing the server certificate and private key must exist. By default, these files are expected to be named On Unix systems, the permissions on If the data directory allows group read access then certificate files may need to be located outside of the data directory in order to conform to the security requirements outlined above. Generally, group access is enabled to allow an unprivileged user to backup the database, and in that case the backup software will not be able to read the certificate files and will likely error. 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. Using a passphrase by default disables the ability to change the server's SSL configuration without a server restart, but seessl_passphrase_command_supports_reload. Furthermore, passphrase-protected private keys cannot be used at all on Windows. The first certificate in It is not necessary to add the root certificate to PostgreSQL reads the system-wideOpenSSL configuration file. By default, this file is named OpenSSL supports a wide range of ciphers and authentication algorithms, of varying strength. While a list of ciphers can be specified in theOpenSSL configuration file, you can specify ciphers specifically for use by the database server by modifyingssl_ciphers in It is possible to have authentication without encryption overhead by using To require the client to supply a trusted certificate, place certificates of the root certificate authorities (CAs) you trust in a file in the data directory, set the parameterssl_ca_file in For a Intermediate certificates that chain up to existing root certificates can also appear in thessl_ca_file file if you wish to avoid storing them on clients (assuming the root and intermediate certificates were created with The There are two approaches to enforce that users provide a certificate during login. The first approach makes use of the The second approach combines any authentication method for Table 18.2 summarizes the files that are relevant to the SSL setup on the server. (The shown file names are default names. The locally configured names could be different.) Table 18.2. SSL Server File Usage The server reads these files at server start and whenever the server configuration is reloaded. OnWindows systems, they are also re-read whenever a new backend process is spawned for a new client connection. If an error in these files is detected at server start, the server will refuse to start. But if an error is detected during a configuration reload, the files are ignored and the old SSL configuration continues to be used. OnWindows systems, if an error in these files is detected at backend start, that backend will be unable to establish an SSL connection. In all these cases, the error condition is reported in the server log. To create a simple self-signed certificate for the server, valid for 365 days, use the followingOpenSSL command, replacing Then do: because the server will reject the file if its permissions are more liberal than this. For more details on how to create your server private key and certificate, refer to theOpenSSL documentation. While a self-signed certificate can be used for testing, a certificate signed by a certificate authority (CA) (usually an enterprise-wide rootCA) should be used in production.18.9.1. Basic Setup#
on
inpostgresql.conf
. The server will listen for both normal andSSL connections on the same TCP port, and will negotiate with any connecting client on whether to useSSL. By default, this is at the client's option; seeSection 20.1 about how to set up the server to require use ofSSL for some or all connections.server.crt
andserver.key
, respectively, in the server's data directory, but other names and locations can be specified using the configuration parametersssl_cert_file andssl_key_file.server.key
must disallow any access to world or group; achieve this by the commandchmod 0600 server.key
. Alternatively, the file can be owned by root and have group read access (that is,0640
permissions). That setup is intended for installations where certificate and key files are managed by the operating system. The user under which thePostgreSQL server runs should then be made a member of the group that has access to those certificate and key files.server.crt
must be the server's certificate because it must match the server's private key. The certificates of“intermediate” certificate authorities can also be appended to the file. Doing this avoids the necessity of storing intermediate certificates on clients, assuming the root and intermediate certificates were created withv3_ca
extensions. (This sets the certificate's basic constraint ofCA
totrue
.) This allows easier expiration of intermediate certificates.server.crt
. Instead, clients must have the root certificate of the server's certificate chain.18.9.2. OpenSSL Configuration#
openssl.cnf
and is located in the directory reported byopenssl version -d
. This default can be overridden by setting environment variableOPENSSL_CONF
to the name of the desired configuration file.postgresql.conf
.Note
NULL-SHA
orNULL-MD5
ciphers. However, a man-in-the-middle could read and pass communications between client and server. Also, encryption overhead is minimal compared to the overhead of authentication. For these reasons NULL ciphers are not recommended.18.9.3. Using Client Certificates#
postgresql.conf
to the new file name, and add the authentication optionclientcert=verify-ca
orclientcert=verify-full
to the appropriatehostssl
line(s) inpg_hba.conf
. A certificate will then be requested from the client during SSL connection startup. (SeeSection 32.19 for a description of how to set up certificates on the client.)hostssl
entry withclientcert=verify-ca
, the server will verify that the client's certificate is signed by one of the trusted certificate authorities. Ifclientcert=verify-full
is specified, the server will not only verify the certificate chain, but it will also check whether the username or its mapping matches thecn
(Common Name) of the provided certificate. Note that certificate chain validation is always ensured when thecert
authentication method is used (seeSection 20.12).v3_ca
extensions). Certificate Revocation List (CRL) entries are also checked if the parameterssl_crl_file orssl_crl_dir is set.clientcert
authentication option is available for all authentication methods, but only inpg_hba.conf
lines specified ashostssl
. Whenclientcert
is not specified, the server verifies the client certificate against its CA file only if a client certificate is presented and the CA is configured.cert
authentication method forhostssl
entries inpg_hba.conf
, such that the certificate itself is used for authentication while also providing ssl connection security. SeeSection 20.12 for details. (It is not necessary to specify anyclientcert
options explicitly when using thecert
authentication method.) In this case, thecn
(Common Name) provided in the certificate is checked against the user name or an applicable mapping.hostssl
entries with the verification of client certificates by setting theclientcert
authentication option toverify-ca
orverify-full
. The former option only enforces that the certificate is valid, while the latter also ensures that thecn
(Common Name) in the certificate matches the user name or an applicable mapping.18.9.4. SSL Server File Usage#
File Contents Effect ssl_cert_file ( $PGDATA/server.crt
)server certificate sent to client to indicate server's identity ssl_key_file ( $PGDATA/server.key
)server private key proves server certificate was sent by the owner; does not indicate certificate owner is trustworthy ssl_ca_file trusted certificate authorities checks that client certificate is signed by a trusted certificate authority ssl_crl_file certificates revoked by certificate authorities client certificate must not be on this list 18.9.5. Creating Certificates#
dbhost.yourdomain.com
with the server's host name:openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=
dbhost.yourdomain.com
"chmod og-rwx server.key