PostgreSQL Connection Pooling
PostgreSQL databases support a limited number of simultaneous direct connections. If your database is approaching or hitting this limit, you can set up connection pooling on Render using PgBouncer.
Using this setup, your other services connect to your PgBouncer instance instead of connecting directly to your database. PgBouncer reuses its pool of active database connections to serve queries from any number of different services.
You can deploy PgBouncer on Render either by declaring its configuration in a
render.yaml blueprint file, or by manually configuring a private service from your dashboard. Both options are covered below.
Create a file named
render.yamlin the root of a Git repository. This file describes your PgBouncer instance, along with the database it serves:
databases: - name: mysite databaseName: mysite user: mysite services: - type: pserv name: pgbouncer runtime: docker plan: standard repo: https://github.com/render-oss/docker-pgbouncer envVars: - key: DATABASE_URL fromDatabase: name: mysite property: connectionString - key: POOL_MODE value: transaction - key: SERVER_RESET_QUERY value: DISCARD ALL - key: MAX_CLIENT_CONN value: 500 - key: DEFAULT_POOL_SIZE value: 50
Commit your changes and push them to GitHub or GitLab.
In the Render Dashboard, go to the Blueprints page and click New Blueprint Instance. Select the repository with the blueprint file (give Render permission to access it if you haven’t already) and click Approve on the next screen.
That’s it! Render creates your database and PgBouncer instance.
You can navigate to your new
pgbouncer service in the dashboard to find the URL that your applications should connect to. You can connect using the internal connection string from your database, replacing the database host with internal hostname of your PgBouncer instance
Create a new PostgreSQL database on Render. Note your database’s internal database URL (you’ll need it in a later step).
Create a new Private Service and point it to Render’s PgBouncer Docker Image:
Select Docker for your private service’s runtime.
Add the following environment variables to the private service:
The internal database URL for the database you created above
That’s it! Save your private service to deploy your PgBouncer instance on Render.