A production-ready, lightweight Docker container for PgBouncer - a lightweight connection pooler for PostgreSQL. Built on Alpine Linux with comprehensive configuration management through environment variables.
- Lightweight: Based on Alpine Linux for minimal resource usage
 - Environment-driven Configuration: Complete configuration through environment variables
 - Automatic Setup: Auto-generates 
pgbouncer.inianduserlist.txton startup - Security-focused: Scram-sha-256 password hashing, non-root execution, proper file permissions
 - Production-ready: Health checks, graceful shutdown, comprehensive logging
 - Easy Deployment: Docker Compose support with PostgreSQL integration
 - Validation: Configuration validation and database connectivity testing
 
- Quick Start
 - Configuration
 - Environment Variables
 - Deployment
 - Usage Examples
 - Monitoring
 - Troubleshooting
 - Contributing
 - License
 
- 
Clone the repository:
git clone <repository-url> cd pgbouncer-docker
 - 
Start the services:
docker-compose up -d
 - 
Connect to PgBouncer:
psql -h localhost -p 6432 -U myuser -d myapp
 
# Build the image
docker build -t my-pgbouncer .
# Run the container
docker run -d \
  --name pgbouncer \
  -p 6432:6432 \
  -e DB_HOST=postgres-server \
  -e DB_PORT=5432 \
  -e DB_USER=myuser \
  -e DB_PASSWORD=mypassword \
  -e DB_NAME=myapp \
  my-pgbouncerThe container is configured entirely through environment variables. The entrypoint script automatically generates the PgBouncer configuration files on startup.
| Variable | Description | Example | 
|---|---|---|
DB_HOST | 
PostgreSQL server hostname | postgres | 
DB_PORT | 
PostgreSQL server port | 5432 | 
DB_USER | 
Database username | myuser | 
DB_PASSWORD | 
Database password | mypassword | 
DB_NAME | 
Database name | myapp | 
| Variable | Description | Default | 
|---|---|---|
PGBOUNCER_PORT | 
PgBouncer listen port | 6432 | 
PGBOUNCER_DATABASE | 
Database pattern | "*" | 
PGBOUNCER_POOL_MODE | 
Pool mode (transaction/session/statement) | transaction | 
PGBOUNCER_AUTH_TYPE | 
Authentication type | md5 | 
PGBOUNCER_MAX_CLIENT_CONN=100          # Maximum client connections
PGBOUNCER_DEFAULT_POOL_SIZE=20         # Default pool size per database
PGBOUNCER_MIN_POOL_SIZE=5              # Minimum pool size
PGBOUNCER_RESERVE_POOL_SIZE=5          # Reserved connections
PGBOUNCER_MAX_DB_CONNECTIONS=50        # Maximum database connections
PGBOUNCER_MAX_USER_CONNECTIONS=50      # Maximum connections per userPGBOUNCER_SERVER_LIFETIME=3600         # Connection lifetime
PGBOUNCER_SERVER_IDLE_TIMEOUT=600      # Server idle timeout
PGBOUNCER_CLIENT_IDLE_TIMEOUT=0        # Client idle timeout (0 = disabled)PGBOUNCER_LOG_CONNECTIONS=1            # Log connections (0/1)
PGBOUNCER_LOG_DISCONNECTIONS=1         # Log disconnections (0/1)
PGBOUNCER_LOG_POOLER_ERRORS=1          # Log pooler errors (0/1)
PGBOUNCER_STATS_PERIOD=60              # Stats logging periodPGBOUNCER_SERVER_RESET_QUERY="DISCARD ALL"
PGBOUNCER_IGNORE_STARTUP_PARAMETERS="extra_float_digits"PGBOUNCER_ADMIN_USERS=myuser           # Users with admin access
PGBOUNCER_STATS_USERS=myuser           # Users with stats accessversion: '3.8'
services:
  postgres:
    image: postgres:18.0
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    volumes:
      - postgres_data:/var/lib/postgresql/18/docker
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myuser -d myapp"]
      interval: 10s
      timeout: 5s
      retries: 5
  pgbouncer:
    build: .
    environment:
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USER: myuser
      DB_PASSWORD: mypassword
      DB_NAME: myapp
      PGBOUNCER_POOL_MODE: transaction
      PGBOUNCER_DEFAULT_POOL_SIZE: 20
    ports:
      - "6432:6432"
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "psql -h localhost -p 6432 -U myuser -d myapp -c 'SELECT 1;' || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
volumes:
  postgres_data:apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgbouncer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pgbouncer
  template:
    metadata:
      labels:
        app: pgbouncer
    spec:
      containers:
      - name: pgbouncer
        image: my-pgbouncer:latest
        ports:
        - containerPort: 6432
        env:
        - name: DB_HOST
          value: "postgres-service"
        - name: DB_PORT
          value: "5432"
        - name: DB_USER
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: username
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-credentials
              key: password
        - name: DB_NAME
          value: "myapp"
        livenessProbe:
          exec:
            command:
            - psql
            - -h
            - localhost
            - -p
            - "6432"
            - -U
            - $(DB_USER)
            - -d
            - $(DB_NAME)
            - -c
            - "SELECT 1;"
          initialDelaySeconds: 30
          periodSeconds: 10
        resources:
          requests:
            memory: "64Mi"
            cpu: "50m"
          limits:
            memory: "128Mi"
            cpu: "100m"
---
apiVersion: v1
kind: Service
metadata:
  name: pgbouncer-service
spec:
  selector:
    app: pgbouncer
  ports:
  - protocol: TCP
    port: 6432
    targetPort: 6432# Connect using psql
psql -h localhost -p 6432 -U myuser -d myapp
# Connection string
postgresql://myuser:mypassword@localhost:6432/myapp# Connect to PgBouncer admin console
psql -h localhost -p 6432 -U myuser -d pgbouncer
# Show active pools
SHOW POOLS;
# Show database configuration
SHOW DATABASES;
# Show connection statistics
SHOW STATS;
# Show current configuration
SHOW CONFIG;
# Reload configuration (after changes)
RELOAD;
# Pause all activity
PAUSE;
# Resume activity
RESUME;import psycopg2
conn = psycopg2.connect(
    host="pgbouncer-host",
    port=6432,
    database="myapp",
    user="myuser",
    password="mypassword"
)const { Pool } = require('pg');
const pool = new Pool({
  host: 'pgbouncer-host',
  port: 6432,
  database: 'myapp',
  user: 'myuser',
  password: 'mypassword',
});import (
    "database/sql"
    _ "github.com/lib/pq"
)
db, err := sql.Open("postgres", 
    "host=pgbouncer-host port=6432 user=myuser password=mypassword dbname=myapp sslmode=disable")import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
    private static final String URL = "jdbc:postgresql://pgbouncer-host:6432/myapp";
    private static final String USER = "myuser";
    private static final String PASSWORD = "mypassword";
    
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
    
    // Using HikariCP connection pool (recommended)
    public static HikariDataSource createDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(URL);
        config.setUsername(USER);
        config.setPassword(PASSWORD);
        config.setMaximumPoolSize(20);
        return new HikariDataSource(config);
    }
}using Npgsql;
using Microsoft.Extensions.DependencyInjection;
// Basic connection
var connectionString = "Host=pgbouncer-host;Port=6432;Database=myapp;Username=myuser;Password=mypassword";
using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync();
// ASP.NET Core DI registration
services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql("Host=pgbouncer-host;Port=6432;Database=myapp;Username=myuser;Password=mypassword"));
// Connection pooling with NpgsqlDataSource (.NET 6+)
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.UseLoggerFactory(loggerFactory);
await using var dataSource = dataSourceBuilder.Build();
await using var connection = await dataSource.OpenConnectionAsync();The container includes built-in health checks:
# Check container health
docker ps
# View health check logs
docker inspect pgbouncer | jq '.[0].State.Health'# View PgBouncer logs
docker logs pgbouncer
# View logs inside container
docker exec pgbouncer tail -f /var/log/pgbouncer/pgbouncer.log-- Connect to pgbouncer admin
psql -h localhost -p 6432 -U myuser -d pgbouncer
-- Pool statistics
SHOW STATS;
-- Pool status
SHOW POOLS;
-- Active clients
SHOW CLIENTS;
-- Active servers
SHOW SERVERS;For Prometheus monitoring, consider using pgbouncer_exporter:
version: '3.8'
services:
  # ... pgbouncer service ...
  
  pgbouncer-exporter:
    image: prometheuscommunity/pgbouncer-exporter:latest
    environment:
      PGBOUNCER_EXPORTER_HOST: pgbouncer
      PGBOUNCER_EXPORTER_PORT: 6432
      PGBOUNCER_EXPORTER_USER: myuser
      PGBOUNCER_EXPORTER_PASSWORD: mypassword
    ports:
      - "9127:9127"# Check database connectivity
docker exec pgbouncer psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "SELECT 1;"
# Check environment variables
docker exec pgbouncer env | grep -E "^(DB_|PGBOUNCER_)"# Verify userlist.txt generation
docker exec pgbouncer cat /etc/pgbouncer/userlist.txt
# Check MD5 hash generation
docker exec pgbouncer sh -c 'echo -n "${DB_PASSWORD}${DB_USER}" | md5sum'-- Check pool utilization
SHOW POOLS;
SHOW STATS;
-- Increase pool sizes if needed# View generated configuration
docker exec pgbouncer cat /etc/pgbouncer/pgbouncer.ini
# Test configuration syntax
docker exec pgbouncer pgbouncer -v /etc/pgbouncer/pgbouncer.iniEnable verbose logging for troubleshooting:
# Add to environment variables
PGBOUNCER_VERBOSE=2
PGBOUNCER_LOG_CONNECTIONS=1
PGBOUNCER_LOG_DISCONNECTIONS=1
PGBOUNCER_LOG_POOLER_ERRORS=1- Transaction: Best for most applications (recommended)
 - Session: Use only if your app requires session state
 - Statement: Highest throughput but limited compatibility
 
- Start with 
default_pool_size = 20-25 - Set 
max_client_connhigher than expected concurrent users - Monitor pool utilization: 
cl_active / cl_waitingratio - Adjust based on your application's connection patterns
 
server_lifetime: Set to handle connection rotation (3600s default)server_idle_timeout: Adjust based on database timeout settingsclient_idle_timeout: Set to 0 for most applications
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
 - Create a feature branch: 
git checkout -b feature-name - Make your changes
 - Test the changes: 
docker-compose up --build - Submit a pull request
 
Please include:
- Docker version
 - Container logs
 - Environment variables (without sensitive data)
 - Steps to reproduce
 
This project is licensed under the MIT License - see the LICENSE file for details.
If this project helped you, please consider giving it a β on GitHub!
Made with β€οΈ for the PostgreSQL community