Sub topic 13
TLS and Restricting Access
This chapter assumes that you know basic PostgreSQL roles, can edit pgbouncer.ini, and understand that an application may connect to PgBouncer instead of connecting directly to PostgreSQL.
PgBouncer can protect two separate network connections:
- The client connection runs from an application to PgBouncer.
- The server connection runs from PgBouncer to PostgreSQL.
Encrypting one connection does not automatically encrypt the other. If PgBouncer and PostgreSQL communicate over a private, trusted host, you might use plain server connections. If they communicate across a network you do not fully trust, configure TLS for both hops.
Restrict where PgBouncer listens
The listen_addr setting controls which local addresses accept TCP connections. Bind PgBouncer to a private address or to localhost when all clients run on the same machine.
[pgbouncer]
listen_addr = 10.20.0.15
listen_port = 6432
auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txt
client_tls_sslmode = require
client_tls_key_file = /etc/pgbouncer/tls/server.key
client_tls_cert_file = /etc/pgbouncer/tls/server.crt
client_tls_ca_file = /etc/pgbouncer/tls/ca.crt
server_tls_sslmode = verify-full
server_tls_ca_file = /etc/pgbouncer/tls/ca.crt
admin_users = pgbouncer_adminclient_tls_sslmode = require makes clients use TLS, but it does not require them to present certificates. Password authentication still identifies the PostgreSQL user.
server_tls_sslmode = verify-full makes PgBouncer verify the PostgreSQL server certificate and its hostname. The hostname in the database entry must match the certificate identity.
[databases]
appdb = host=db.internal.example port=5432 dbname=appdbThe certificate authority file contains certificates that PgBouncer trusts. Protect the private key with filesystem permissions so only the PgBouncer operating-system user can read it.
Use network controls as a second boundary
TLS encrypts traffic and authenticates peers when certificate verification is enabled. It does not replace network access control. Add firewall rules that allow port 6432 only from application networks. Allow PostgreSQL port 5432 only from PgBouncer hosts and approved administration networks.
Also restrict the admin console. Do not expose it to application subnets. The special pgbouncer database provides administrative commands, so an exposed or weakly protected admin account can reveal connection information and change pool behavior.
Certificate choices and common failures
A typical deployment uses a server certificate for PgBouncer and a separate PostgreSQL server certificate. Clients validate the PgBouncer certificate against their copy of the certificate authority. PgBouncer validates PostgreSQL against its own CA file.
| Setting | Protects | Main check |
|---|---|---|
| `client_tls_*` | Application to PgBouncer | Client validates PgBouncer |
| `server_tls_*` | PgBouncer to PostgreSQL | PgBouncer validates PostgreSQL |
| Firewall rules | Network reachability | Source and destination addresses |
Common errors include an unreadable key, an expired certificate, a hostname mismatch with verify-full, and a missing CA certificate. Check PgBouncer's log and test each hop separately. A successful connection to port 6432 does not prove that PgBouncer can establish its backend connection to PostgreSQL.
Exercise
An application must connect to PgBouncer with encryption, and PgBouncer must verify PostgreSQL's certificate. Which settings are central?
Show answer
Use clienttlssslmode = require with client certificate and key settings, use servertlssslmode = verify-full with servertlsca_file, and make the database host name match PostgreSQL's certificate identity.