57.1. Usage #

57.1.1. Command line switches
57.1.2. Admin console

The basic setup and usage of PgBouncer is as follows:

1. Create a pgbouncer.ini file:

                [databases]
                template1 = host=localhost port=5432 dbname=template1

                [pgbouncer]
                listen_port = 6432
                listen_addr = localhost
                auth_type = md5
                auth_file = userlist.txt
                logfile = pgbouncer.log
                pidfile = pgbouncer.pid
                admin_users = someuser
            

2. Create a userlist.txt file that contains the users allowed in:

                "someuser" "same_password_as_in_server"
            

3. Launch pgbouncer:

                $ pgbouncer -d pgbouncer.ini
            

4. Have your application (or the psql client) connect to pgbouncer instead of directly to the PgES server:

                $ psql -p 6432 -U someuser template1
            

5. Manage pgbouncer by connecting to the special administration database pgbouncer and issuing SHOW HELP; to begin:

                $ psql -p 6432 -U someuser pgbouncer
                pgbouncer=# SHOW HELP;
                NOTICE:  Console usage
                DETAIL:
                SHOW [HELP|CONFIG|DATABASES|FDS|POOLS|CLIENTS|SERVERS|SOCKETS|LISTS|VERSION|...]
                SET key = arg
                RELOAD
                PAUSE
                SUSPEND
                RESUME
                SHUTDOWN
                [...]
            

6. If you made changes to the pgbouncer.ini file, you can reload it with:

                pgbouncer=# RELOAD;
            

57.1.1. Command line switches #

                    pgbouncer [-d][-R][-v][-u user] <pgbouncer.ini>
                    pgbouncer -V|-h
                

-d, --daemon

Run in the background. Without it, the process will run in the foreground.

-u USERNAME, --user=USERNAME

Switch to the given user on startup.

-v, --verbose

Increase verbosity. Can be used multiple times.

-q, --quiet

Be quiet: do not log to stderr. This does not affect logging verbosity, only that stderr is not to be used. For use in init.d scripts.

-V, --version

Show version.

-h, --help

Show short help.

--regservice

Win32: Register pgbouncer to run as Windows service. The service_name configuration parameter value is used as the name to register under.

--unregservice

Win32: Unregister Windows service

57.1.2. Admin console #

The console is available by connecting as normal to the database pgbouncer:

                    $ psql -p 6432 pgbouncer
                

Only users listed in the configuration parameters admin_users or stats_users are allowed to log in to the console. (Except when auth_type=any, then any user is allowed in as a stats_user.) Additionally, the user name pgbouncer is allowed to log in without password, if the login comes via the Unix socket and the client has same Unix user UID as the running process. The admin console currently only supports the simple query protocol.

SHOW STATS

Shows statistics. In this and related commands, the total figures are since process start, the averages are updated every stats_period.

SHOW STATS_TOTALS

Subset of SHOW STATS showing the total values (total_).

SHOW STATS_AVERAGES

Subset of SHOW STATS showing the average values (avg_).

SHOW TOTALS

Like SHOW STATS but aggregated across all databases.

SHOW SERVERS

Shuw underlying PgES servers

SHOW CLIENTS

Show connected clients

SHOW POOLS

Show connection pools for each couple of (database, user).

SHOW PEER_POOLS

A new peer_pool entry is made for each configured peer.

SHOW LISTS

Show internal information.

SHOW DATABASES

Show registered databases.

SHOW SOCKETS, SHOW ACTIVE_SOCKETS

Shows low-level information about sockets or only active sockets. This includes the information shown under SHOW CLIENTS and SHOW SERVERS as well as other more low-level information.

SHOW CONFIG

Show the current configuration settings

SHOW MEM

Shows low-level information about the current sizes of various internal memory allocations

SHOW DNS_HOSTS

Show host names in DNS cache.

SHOW DNS_ZONES

Show DNS zones in cache.

SHOW VERSION

Show the PgBouncer version string.

SHOW STATE

Show the PgBouncer state settings. Current states are active, paused and suspended.

PAUSE [db]

PgBouncer tries to disconnect from all servers. Disconnecting each server connection waits for that server connection to be released according to the server pool’s pooling mode (in transaction pooling mode, the transaction must complete, in statement mode, the statement must complete, and in session pooling mode the client must disconnect). The command will not return before all server connections have been disconnected. To be used at the time of database restart. If database name is given, only that database will be paused. New client connections to a paused database will wait until RESUME is called.

DISABLE db

Reject all new client connections on the given database.

ENABLE db

Allow new client connections after a previous DISABLE command.

RECONNECT [db]

Close each open server connection for the given database, or all databases, after it is released (according to the pooling mode), even if its lifetime is not up yet. New server connections can be made immediately and will connect as necessary according to the pool size settings.

This command is useful when the server connection setup has changed, for example to perform a gradual switchover to a new server. It is not necessary to run this command when the connection string in pgbouncer.ini has been changed and reloaded (see RELOAD) or when DNS resolution has changed, because then the equivalent of this command will be run automatically. This command is only necessary if something downstream of PgBouncer routes the connections.

After this command is run, there could be an extended period where some server connections go to an old destination and some server connections go to a new destination. This is likely only sensible when switching read-only traffic between read-only replicas, or when switching between nodes of a multimaster replication setup. If all connections need to be switched at the same time, PAUSE is recommended instead. To close server connections without waiting (for example, in emergency failover rather than gradual switchover scenarios), also consider KILL.

KILL db

Immediately drop all client and server connections on given database. New client connections to a killed database will wait until RESUME is called.

SUSPEND

All socket buffers are flushed and PgBouncer stops listening for data on them. The command will not return before all buffers are empty. To be used at the time of PgBouncer online reboot. New client connections to a suspended database will wait until RESUME is called.

RESUME [db]

Resume work from previous KILL, PAUSE, or SUSPEND command.

SHUTDOWN

The PgBouncer process will exit.

RELOAD

The PgBouncer process will reload its configuration files and update changeable settings. This includes the main configuration file as well as the files specified by the settings auth_file and auth_hba_file.

PgBouncer notices when a configuration file reload changes the connection parameters of a database definition. An existing server connection to the old destination will be closed when the server connection is next released (according to the pooling mode), and new server connections will immediately use the updated connection parameters.

WAIT_CLOSE [db]

Wait until all server connections, either of the specified database or of all databases, have cleared the “close_needed” state (see SHOW SERVERS). This can be called after a RECONNECT or RELOAD to wait until the respective configuration change has been fully activated, for example in switchover scripts.

SET key = arg

Changes a configuration setting (see also SHOW CONFIG)