PgBouncer: A Simple Guide for PostgreSQL Connection Pooling

Gabriel Xará
3 min readMay 24, 2024

--

When managing a high-demand application, the latency of database calls is a critical factor that can significantly impact performance. Additionally, maintaining multiple database connections can be resource-intensive, consuming substantial memory. In these scenarios, connection pooling is an effective strategy to mitigate both application and database performance issues.

For PostgreSQL databases, PgBouncer stands out as a leading connection pooler, optimizing the management of database connections. It minimizes memory usage and reduces query latency, enhancing overall system efficiency.

This guide is a straightforward tutorial to install and configure PgBouncer. Furthermore.

What is PgBouncer?

PgBouncer is a lightweight yet powerful connection pooler for PostgreSQL. It addresses a critical challenge in database management: the high cost of opening and maintaining multiple database connections. Each new connection consumes approximately 10MB of memory, which can quickly escalate in systems with numerous concurrent connections, leading to increased latency and resource depletion.

To mitigate these issues, PgBouncer maintains a pool of active connections that can be reused by incoming requests, thereby optimizing resource usage and improving performance. While it is possible to implement connection pooling within the application itself, doing so can lead to idle connections if the application crashes or exits unexpectedly. PgBouncer provides a centralized solution to manage these connections efficiently.

Since PgBouncer acts as a middleman for database connections, the application code usually doesn’t require any changes. However, it is crucial to ensure that the application closes connections properly after use so they can be returned to the pool.

Setting Up PgBouncer

Installation

For those new to PgBouncer or setting it up on a fresh machine, the installation process is straightforward. First, download the latest release from the PgBouncer downloads page

wget https://www.pgbouncer.org/downloads/files/1.22.1/pgbouncer-1.22.1.tar.gz

Next, build the binaries from the source code. Detailed instructions for compilation and installation can be found in the official PgBouncer documentation.

Configuration

The configuration of PgBouncer involves setting up two key files: pgbouncer.ini and userlist.txt. These files should be securely stored, as they contain sensitive credentials. In my case, I opted to store them in Secret Manager, but that's up to you!

Here’s a sample pgbouncer configuration:

[databases]
template1 = host=YOUR_DATABASE_IP port=YOUR_DATABASE_PORT dbname=YOUR_DATABASE_NAME

[pgbouncer]
listen_port = 6432
listen_addr = 0.0.0.0
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt ; Path to userlist file
logfile = /etc/pgbouncer/pgbouncer.log ; Path to pgbouncer logs
pidfile = pgbouncer.pid
admin_users = someuser
pool_mode = session
max_client_conn = 100
default_pool_size = 20
min_pool_size = 15

Here is an example of a valid userlist.txt file:

"someuser"    "password_for_someuser"
"anotheruser" "password_for_another_user"

Starting the PgBouncer Service

Starting the PgBouncer service is pretty simple, just by running the following command:

pgbouncer -d -R /etc/pgbouncer/pgbouncer.ini

The `-d` flag runs PgBouncer as a daemon process, and `-R` restarts the service if it is already running.

Conclusion

PgBouncer is an indispensable tool for managing PostgreSQL connections efficiently. By reducing memory consumption and improving query latency, it helps maintain the performance and reliability of your database systems. This guide provides a comprehensive overview of PgBouncer’s installation, configuration, and integration, empowering you to harness its full potential in your database management practices.

For further details and updates, refer to the official PgBouncer documentation

--

--

Gabriel Xará
Gabriel Xará

Written by Gabriel Xará

Software Engineer and Entrepreneur. Passionate about education and technology. Trying to revolutionize learning methodologies through cmaps.io

No responses yet