Enabling SSL/TLS Sessions In PgBouncer

June 28, 2021
Author
Robert Bernier
Share this Post:

Enabling SSL/TLS Sessions in PgBouncerPgBouncer is a great piece of technology. Over the years, I have put it to good use in any number of situations requiring a particular type of control over application processes connecting to a Postgres data cluster. However, configuration can sometimes be a challenge.

Today, I want to demonstrate one way to conduct a connection session using Secure Socket Layer, or SSL/TLS.

For our purposes, we are going to make the following assumptions:

  • We are using a typical installation found on CentOS 7.
  • PostgreSQL version 13 is used, but essentially any currently supported version of Postgres will work.

Here are the steps for enabling SSL connection sessions:

  1. Set up Postgres
    • Install RPM packages.
    • Set up remote access.
    • Create a role with remote login privileges.
  2. Set up PgBouncer
    • Install RPM packages.
    • Set up the minimal configuration permitting remote login without SSL.
  3. Generate SSL/TLS private keys and certificates
    • TLS certificate for Postgres.
    • TLS certificate for PgBouncer.
    • Create a Certificate Authority, or CA, capable of signing the aforementioned certificates.
  4. Configure SSL-encrypted sessions
    • Postgres.
    • PgBouncer.

Step 1: Set Up Postgres

Setting up your Postgres server is straightforward.

Add the appropriate repository for Postgres version 13:

Initialize the data cluster:

Edit the data cluster configuration files, pg_hba.conf and postgresql.auto.conf. Note that both IPv4 and IPv6 protocols have been configured.

Step 2: Set Up PgBouncer

There is not much to this first PgBouncer configuration iteration. All that is required is validating that a connection can be made before updating the SSL configuration.

Note: Best practice recommends hashing passwords when editing userlist.txt. For our purposes, to keep things simple, we will leave the passwords in clear text.

Step 3: Set Up SSL/TLS Certificates

Create a root certificate fit for signing certificate requests:

Create two sets of keys and certificate requests, one for PgBouncer and one for Postgres. The certificate requests are signed with the newly created root certificate:

Validate the signed certificates:

Step 4: Install Certificates and Configure Servers for SSL Connectivity

Update ownership for keys and certificates:

Move keys and certificates into their respective locations:

Update pgbouncer.ini:

Update postgresql.auto.conf:

Validate SSL connectivity:

Caveat: A Few Words About Those Certificates

Using certificates signed by a Certificate Authority gives you the ability to go further than simply enabling SSL sessions. For example, although not covered here, you can dispense with passwords and instead rely on the certificate’s identity as the main authentication mechanism.

Remember: you can still conduct SSL sessions using self-signed certificates. However, you cannot leverage the other validation methods in Postgres.

Finally, do not forget to save the root certificate’s private key, root.key, in a safe place.

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together.

Download Percona Distribution for PostgreSQL Today!

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
William Walker
William Walker
4 years ago

Nice post, thanks!

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved