Sub topic 4
Your first `pgbouncer.ini`
Assume you know basic SQL, can use psql, understand PostgreSQL roles and max_connections, and can edit files on a Linux system. PgBouncer reads its main configuration from pgbouncer.ini. The file has sections. The [databases] section defines where PostgreSQL databases are located. The [pgbouncer] section controls the pooler itself.
A minimal configuration
This example accepts up to 1,000 client connections but creates at most 20 PostgreSQL server connections for each database and user pool.
[databases]
appdb = host=127.0.0.1 port=5432 dbname=appdb
[pgbouncer]
listen_addr = 127.0.0.1
listen_port = 6432
auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txt
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20
reserve_pool_size = 5
reserve_pool_timeout = 3
admin_users = pgbouncer_admin
logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pidThe application connects to port 6432, which is PgBouncer's listening port. PgBouncer connects to PostgreSQL on port 5432. The name before the equals sign, appdb, is the database name that clients request through PgBouncer.
| Setting | Meaning |
|---|---|
| `listen_addr` | Address where PgBouncer accepts client connections |
| `listen_port` | Port where PgBouncer listens |
| `pool_mode` | Rule for releasing server connections |
| `max_client_conn` | Maximum number of client connections PgBouncer accepts |
| `default_pool_size` | Maximum normal server connections per pool |
| `reserve_pool_size` | Extra server connections available during a wait |
| `reserve_pool_timeout` | Seconds a client waits before using the reserve pool |
A pool is normally separated by database and user. Therefore, default_pool_size = 20 does not necessarily mean 20 server connections for the entire PgBouncer process. If several users connect to several databases, the possible total is larger.
How a connection travels
The application sees PgBouncer as the database server. PgBouncer keeps client sockets open and assigns an available PostgreSQL server connection when a transaction needs one.
Authentication and the admin user
With auth_file, PgBouncer reads permitted usernames and password hashes from the configured file. A line uses this format:
"app_user" "SCRAM-SHA-256-password-hash"
"pgbouncer_admin" "SCRAM-SHA-256-password-hash"The pgbouncer_admin role can connect to the special database named pgbouncer and run administrative commands. The role must also be available through the selected authentication method.
After changing the file, start PgBouncer with the configuration path used by your installation. Test the application path separately from the PostgreSQL path:
psql -h 127.0.0.1 -p 6432 -U app_user appdbIf you change pgbouncer.ini while PgBouncer is running, use RELOAD in the admin console instead of restarting immediately. A reload applies supported settings without dropping existing client connections.
Exercise
Which setting determines the maximum normal number of PostgreSQL server connections for one database-and-user pool?
Show answer
default_pool_size.