Sub topic 13
TLS and restricting access
This chapter assumes you can write a basic pgbouncer.ini, you know how auth_file or auth_query verify logins, and you can create a certificate with openssl. It covers how to encrypt traffic through PgBouncer and how to limit who can reach it.
Two hops, two TLS settings
PgBouncer splits every connection into two separate network hops. Each hop has its own TLS settings, and enabling one does not enable the other.
- The client hop runs from your application to PgBouncer. The
client_tls_*settings control it, and PgBouncer acts as the TLS server. - The server hop runs from PgBouncer to PostgreSQL. The
server_tls_*settings control it, and PgBouncer acts as the TLS client.
Because the hops are independent, a client can use TLS on the first hop while PgBouncer talks to PostgreSQL in plaintext, or the reverse. PgBouncer terminates the client's TLS session. It does not pass the client's encrypted stream through to PostgreSQL. If PgBouncer runs on the same host as PostgreSQL and uses 127.0.0.1, the server hop never leaves the machine, so TLS there matters less. When PgBouncer and PostgreSQL are on different hosts, encrypt both hops.
Encrypting client connections
PgBouncer needs a private key and a certificate to accept TLS. For a test setup, you can create a self-signed pair:
openssl req -x509 -newkey rsa:4096 -nodes -days 365 \
-keyout /etc/pgbouncer/pgbouncer.key \
-out /etc/pgbouncer/pgbouncer.crt \
-subj "/CN=pgbouncer.internal.example.com"
chown pgbouncer:pgbouncer /etc/pgbouncer/pgbouncer.key
chmod 600 /etc/pgbouncer/pgbouncer.keyIn production, use a certificate signed by your internal certificate authority (CA) so clients can verify it. Then add these settings to the [pgbouncer] section:
client_tls_sslmode = require
client_tls_key_file = /etc/pgbouncer/pgbouncer.key
client_tls_cert_file = /etc/pgbouncer/pgbouncer.crt
client_tls_protocols = secureclient_tls_sslmode accepts the same values as PostgreSQL's sslmode, but from the server's point of view:
| Value | Behavior on the client hop |
|---|---|
| `disable` | Plaintext only. This is the default. |
| `allow` | TLS is accepted if the client asks for it, but plaintext also works. |
| `prefer` | Same as `allow` in practice: PgBouncer offers TLS but does not insist. |
| `require` | Clients must use TLS. Plaintext connections are rejected. |
| `verify-ca` | Requires TLS and a client certificate signed by `client_tls_ca_file`. |
| `verify-full` | Like `verify-ca`. PgBouncer also checks the client certificate's name against the connecting username. |
The default of disable surprises many people. Installing a certificate alone does nothing until you set client_tls_sslmode. The client_tls_protocols = secure setting limits connections to TLS 1.2 and newer, which is the sensible default for current deployments.
On the application side, ask for the strictest mode that your certificate supports. For a CA-signed certificate, that is verify-full:
DATABASE_URL="postgresql://[email protected]:6432/appdb?sslmode=verify-full&sslrootcert=/etc/ssl/internal-ca.crt"With sslmode=require the client encrypts the traffic but does not check who it is talking to, so it does not protect against an impostor on the network. Use verify-full where you can.
Encrypting PgBouncer's connection to PostgreSQL
The server hop uses the server_tls_* settings. Its default is prefer, so PgBouncer will already use TLS if PostgreSQL offers it. It will also silently fall back to plaintext if TLS fails to negotiate. To make encryption mandatory and verify the server's identity, set:
server_tls_sslmode = verify-full
server_tls_ca_file = /etc/pgbouncer/postgres-ca.crt
server_tls_protocols = securePostgreSQL must have TLS enabled (ssl = on with a server certificate). PgBouncer then checks the certificate's hostname against the host= value in the [databases] entry. If the host is an IP address and the certificate only lists a DNS name, verify-full fails. Use the DNS name in host=.
PostgreSQL can also demand a client certificate from PgBouncer. In that case, add server_tls_cert_file and server_tls_key_file to PgBouncer's configuration.
Restricting who can connect
Encryption protects data on the wire. It does not decide who gets in. PgBouncer gives you several layers for that, and you should use more than one.
Listen only where needed
listen_addr controls which interfaces accept connections. The default is 127.0.0.1. Set it to a specific private address, not *, unless a firewall controls access. You can also use a Unix socket and skip TCP entirely for applications on the same host:
listen_addr = 10.0.1.15
unix_socket_dir = /var/run/postgresql
unix_socket_mode = 0777Back this up with a firewall or security group that allows port 6432 only from the application subnets.
Use an HBA file
Setting auth_type = hba makes PgBouncer read a file in the same format as PostgreSQL's pg_hba.conf. This lets you require TLS per database and per network, and reject everything else. PgBouncer's HBA file supports the connection types local, host, hostssl, and hostnossl.
auth_type = hba
auth_hba_file = /etc/pgbouncer/pgb_hba.conf
auth_file = /etc/pgbouncer/userlist.txtRules are checked from top to bottom, and the first match wins:
# TYPE DATABASE USER ADDRESS METHOD
local pgbouncer pgbouncer_admin peer
hostssl appdb app_user 10.0.1.0/24 scram-sha-256
hostssl appdb report_user 10.0.2.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectHere app_user may connect to appdb only over TLS and only from 10.0.1.0/24. A plaintext attempt from that subnet matches no hostssl rule, so it falls through to the final reject line. The auth_file still holds the password hashes that the scram-sha-256 method checks against. The HBA file only decides which method applies.
Authenticate with client certificates
For machine-to-machine access, PgBouncer can skip passwords. Set client_tls_ca_file and use the cert method in the HBA file, or set auth_type = cert. The certificate's common name must match the PostgreSQL username the client is requesting. This removes password hashes from the picture for those clients, at the cost of running a certificate authority.
Protecting the admin console
The admin console can pause pools, kill connections, and reload configuration, so treat it as a privileged interface.
admin_userslists the users who may run every console command. Keep this list short, and keep it separate from application users.stats_userslists users who may only run read-onlySHOWcommands. Give this role to monitoring tools.- Both lists need matching entries in
auth_fileorauth_query. A name inadmin_userswith no credentials cannot log in. - The console is reached through the virtual database
pgbouncer, so the HBA rules above control who can reach it. The example HBA file lets onlypgbouncer_adminin, through the local socket withpeerauthentication.
admin_users = pgbouncer_admin
stats_users = metrics_readerRestrict the files too. userlist.txt holds password hashes, so make it readable only by the PgBouncer service account:
chown pgbouncer:pgbouncer /etc/pgbouncer/userlist.txt /etc/pgbouncer/pgb_hba.conf
chmod 600 /etc/pgbouncer/userlist.txtVerifying the result
After a RELOAD (or a restart, since some TLS settings are read at startup, so check the PgBouncer documentation for your version), connect through the pooler and check the session from PostgreSQL's side:
SELECT ssl, version, cipher
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();This shows whether the server hop is encrypted, because PostgreSQL sees PgBouncer as its client. For the client hop, connect with psql "sslmode=require host=... port=6432 dbname=appdb user=app_user" and look at the connection banner. The admin console's SHOW CLIENTS; and SHOW SERVERS; commands also include a tls column that describes the TLS state of each connection.
Also test the negative cases: a plaintext connection must be rejected, and a connection from an address outside the allowed subnet must fail.
Common mistakes
- Setting a certificate path without
client_tls_sslmode. The defaultdisableignores it. - A private key that PgBouncer's service account cannot read. PgBouncer logs an error and refuses TLS connections.
- Using
verify-fullon the server hop with an IP address inhost=, which fails the hostname check. - Leaving
listen_addr = *on a host with a public interface and no firewall. - Adding a user to
admin_usersbut not toauth_file, which leaves that user unable to log in.
Exercise
Your application connects to PgBouncer with sslmode=require, and PgBouncer is configured with clienttlssslmode = require. PgBouncer runs on a separate host from PostgreSQL, and you have not set any servertls* options. Is all traffic between the application and PostgreSQL encrypted? What do you change so that it is, and so that PgBouncer verifies PostgreSQL's identity?
Show answer
Not necessarily. The client hop is encrypted, but servertlssslmode defaults to prefer, so the server hop uses TLS only if PostgreSQL offers it and silently falls back to plaintext otherwise. Enable ssl on PostgreSQL, then set servertlssslmode = verify-full and servertlsca_file to the CA that signed PostgreSQL's certificate. Use the certificate's DNS name in the host= value of the [databases] entry so the hostname check passes.
Exercise
Write the HBA lines that allow appuser to reach appdb only over TLS from 10.0.1.0/24 using scram-sha-256, and reject every other connection. Which authtype value makes PgBouncer read this file?
Show answer
hostssl appdb appuser 10.0.1.0/24 scram-sha-256, followed by host all all 0.0.0.0/0 reject. Rules are matched top to bottom, so the reject line must come last. Set authtype = hba and point authhbafile at the file. Keep auth_file configured so PgBouncer still has the password hashes to check.