Deploy PgBouncer 1.26 with PostgreSQL 18

Postgres 18 behind a PgBouncer connection pool, private and ready to use.

Deploy PgBouncer 1.26 with PostgreSQL 18

Deploy and Host PgBouncer with Railway

PgBouncer is the lightweight, battle-tested connection pooler for PostgreSQL. This template deploys PgBouncer 1.26 in front of a PostgreSQL 18 database on Railway's private network, so many app instances, workers or serverless functions can share a small number of real database connections. Point your apps at one pooled URL and you are done.

About Hosting PgBouncer

Every Postgres connection is a server process that costs memory, and Postgres allows only about 100 by default. Apps with many replicas, background workers or short-lived connections exhaust that quickly. PgBouncer sits between the apps and Postgres, accepts thousands of client connections and hands each transaction a pooled server connection. Hosting it means configuring pools and authentication, keeping it close to the database, and monitoring that it is up. This template generates the PgBouncer configuration from variables, authenticates clients against the real Postgres users (no separate password list), exposes a private health endpoint for Railway, and keeps both services off the public internet.

Common Use Cases

  • Many app replicas or workers sharing one Postgres without hitting max_connections
  • Serverless or short-lived connections (cron jobs, functions, CLIs) that open a connection per request
  • Smoothing connection spikes during deploys and autoscaling
  • Pooling an existing Railway Postgres without changing the database
  • Watching connection and pool statistics from PgBouncer's admin console

Dependencies for PgBouncer Hosting

  • PgBouncer (edoburu/pgbouncer image, built from the official release)
  • A PostgreSQL server to pool (bundled: PostgreSQL 18)

Deployment Dependencies

Implementation Details

ServiceImagePurposePublic
PgBounceredoburu/pgbouncer:v1.26.1-p0 (digest-pinned) + config/health scriptPooler on port 6432, health on 8080No (private network only)
Postgresghcr.io/railwayapp-templates/postgres-ssl:18.7 (digest-pinned)Database, volume /var/lib/postgresql/dataNo

Connect your apps: set your app's database variable to ${{PgBouncer.DATABASE_URL}}, which is postgresql://{user}:{password}@pgbouncer.railway.internal:6432/{database}. Any Postgres user and database works through the pooler with its normal password: PgBouncer looks passwords up in Postgres (auth_query, SCRAM pass-through), so new users need no extra setup. Run migrations directly against ${{Postgres.DATABASE_URL}}: migration tools often rely on session features (advisory locks) that transaction pooling does not keep.

Pool settings (variables on the PgBouncer service, applied at the next deploy; invalid values fall back to the default with a log warning):

VariableDefaultMeaning
PGBOUNCER_POOL_MODEtransactionServer connection is released after each transaction. Use session for apps that need session state (LISTEN/NOTIFY, advisory locks, SET without LOCAL).
PGBOUNCER_DEFAULT_POOL_SIZE20Server connections per database/user pair
PGBOUNCER_MAX_DB_CONNECTIONS80Cap per database; keep it below Postgres max_connections (100)
PGBOUNCER_RESERVE_POOL_SIZE5Extra connections for bursts
PGBOUNCER_MAX_CLIENT_CONN1000Total client connections accepted
PGBOUNCER_MAX_PREPARED_STATEMENTS200Prepared statements work in transaction mode
PGBOUNCER_QUERY_WAIT_TIMEOUT120Seconds a client may wait for a free connection

Admin console: from a service in the project, psql "postgresql://postgres:{password}@pgbouncer.railway.internal:6432/pgbouncer" -c "SHOW POOLS" (also SHOW STATS, SHOW CLIENTS).

Health: GET /healthz on port 8080 (private) answers 200 while PgBouncer accepts connections and reports whether Postgres is reachable.

Pool an existing Railway Postgres: set POSTGRES_HOST to that service's private domain (${{OtherPostgres.RAILWAY_PRIVATE_DOMAIN}}), and AUTH_USER / AUTH_PASSWORD to its superuser credentials; then delete the bundled Postgres if you do not need it. For a database outside Railway set PGBOUNCER_SERVER_TLS=require.

Access from outside Railway: add a TCP proxy to the PgBouncer service (port 6432) and set PGBOUNCER_CLIENT_TLS=require; clients then connect with sslmode=require (the certificate is self-signed).

Scaling and upgrades: PgBouncer is single-threaded and handles thousands of clients per instance; raise pool sizes before adding instances. To upgrade, change the image tag and digest in services/pgbouncer/Dockerfile of your repo copy.

This is a community template and is not affiliated with the PgBouncer project or the PostgreSQL Global Development Group.

Why Deploy PgBouncer on Railway?

Railway runs PgBouncer next to your database on a private network, with healthchecks, logs and restarts handled for you. Your apps get a pooled connection string by reference, and you can scale app replicas without running out of Postgres connections.


Template Content

More templates in this category

View Template
RustFS | (Just Updated) S3 Object Store, Public URL and Keys Set
S3-compatible object store, keys and public URL preset, volume kept

SuperSlowSloth
0
View Template
TimescaleDB
A PostgreSQL-based time-series database for analytics on Railway.

Rakit
1
View Template
Neo4j 2026.10 Community
Neo4j graph database with APOC, Browser, Bolt TCP proxy and a volume

My Projects
0