Configure Prisma Client with PgBouncer
Configure Prisma Client with PgBouncer and other poolers: when to use pgbouncer=true, required transaction mode, prepared statements, and Prisma Migrate workarounds
An external connection pooler like PgBouncer holds a connection pool to the database, and proxies incoming client connections by sitting between Prisma Client and the database. This reduces the number of processes a database has to handle at any given time.
Usually, this works transparently, but some connection poolers only support a limited set of functionality. One common feature that external connection poolers do not support are named prepared statements, which Prisma ORM uses. For these cases, Prisma ORM can be configured to behave differently.
Questions answered in this page
- How do I configure Prisma with PgBouncer?
- Do I need
pgbouncer=true, and if so, when? - How does Prisma Migrate work with PgBouncer?
Looking for an easy, infrastructure-free solution? Try Prisma Accelerate! It requires little to no setup and works seamlessly with all databases supported by Prisma ORM.
Ready to begin? Get started with Prisma Accelerate by clicking here.
PgBouncer
Set PgBouncer to transaction mode
For Prisma Client to work reliably, PgBouncer must run in Transaction mode.
Transaction mode offers a connection for every transaction – a requirement for the Prisma Client to work with PgBouncer.
Add pgbouncer=true for PgBouncer versions below 1.21.0
We recommend not setting pgbouncer=true in the database connection string if you're using PgBouncer 1.21.0 or later.
To use Prisma Client with PgBouncer, add the ?pgbouncer=true flag to the PostgreSQL connection URL:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?pgbouncer=truePORT specified for PgBouncer pooling is sometimes different from the default 5432 port. Check your database provider docs for the correct port number.
Configure max_prepared_statements in PgBouncer to be greater than zero
Prisma uses prepared statements, and setting max_prepared_statements to a value greater than 0 enables PgBouncer to use those prepared statements.
PORT specified for PgBouncer pooling is sometimes different from the default 5432 port. Check your database provider docs for the correct port number.
Prisma Migrate and PgBouncer workaround
Prisma Migrate uses database transactions to check out the current state of the database and the migrations table. However, the Schema Engine is designed to use a single connection to the database, and does not support connection pooling with PgBouncer. If you attempt to run Prisma Migrate commands in any environment that uses PgBouncer for connection pooling, you might see the following error:
Error: undefined: Database error
Error querying the database: db error: ERROR: prepared statement "s0" already existsTo work around this issue, configure a direct connection for Prisma CLI commands in prisma.config.ts, while Prisma Client continues to use the PgBouncer URL via a driver adapter.
# PgBouncer (pooled) connection string used by Prisma Client.
DATABASE_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE?pgbouncer=true"
# Direct database connection string used by Prisma CLI.
DIRECT_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE"import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DIRECT_URL"),
},
});import { PrismaClient } from "../prisma/generated/client";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
export const prisma = new PrismaClient({ adapter });With this setup, PgBouncer stays in the path for runtime traffic, while Prisma CLI commands (prisma migrate dev, prisma db push, prisma db pull, and so on) always use the direct connection string defined in prisma.config.ts.
PgBouncer with different database providers
There are sometimes minor differences in how to connect directly to a Postgres database that depend on the provider hosting the database.
Below are links to information on how to set up these connections with providers who have setup steps not covered here in our documentation:
- Connecting directly to a PostgreSQL database hosted on Digital Ocean
- Connecting directly to a PostgreSQL database hosted on ScaleGrid
Supabase Supavisor
Supabase's Supavisor behaves similarly to PgBouncer. You can add ?pgbouncer=true to your connection pooled connection string available via your Supabase database settings.
Other external connection poolers
Although Prisma ORM does not have explicit support for other connection poolers, if the limitations are similar to the ones of PgBouncer you can usually also use pgbouncer=true in your connection string to put Prisma ORM in a mode that works with them as well.