skip to content

With Prisma 7 behind PgBouncer in transaction mode, why can prisma migrate fail through the pooled URL, and how do you set up the two connection strings?

level: seniorimportance: should knowfreq 25%

answer

  1. the CLI is not the client
  2. one dedicated connection for migrations
  3. prepared statement s0
  4. the config URL goes direct

basics

~10 s

Prisma Migrate's Schema Engine expects a single dedicated connection and can fail through PgBouncer with 'prepared statement "s0" already exists'. Point datasource.url in prisma.config.ts at a direct URL and give PrismaPg the pooled one.

solid answer

~40 s

The Prisma docs require PgBouncer to run in **transaction mode** for Prisma Client, and runtime queries through `PrismaPg` work that way. Prisma Migrate is different: its Schema Engine uses transactions to read the database state and the migrations table, is designed around a single connection, and does not support pooling through PgBouncer, so `prisma migrate dev` or `deploy` through the pooled URL can fail with `prepared statement "s0" already exists`. Prisma 7 removed `directUrl`; every CLI command now uses `datasource.url` in `prisma.config.ts`. So set that to a direct URL, for example `env("DIRECT_URL")`, and construct `PrismaPg` with the pooled `DATABASE_URL`. The docs also ask for PgBouncer's `max_prepared_statements` above zero, and reserve `?pgbouncer=true` for PgBouncer older than 1.21.0.

code

bash · 5 lines
bash
# Pooled connection through PgBouncer, used by Prisma Client at runtime
DATABASE_URL="postgres://app:secret@pgbouncer:6432/shop"

# Direct connection to PostgreSQL, used by the Prisma CLI
DIRECT_URL="postgres://app:secret@postgres:5432/shop"

go deeper

for a junior

Remember the pairing: the pooled URL goes to the PrismaPg adapter, and the direct URL goes in prisma.config.ts for migrations.

for a middle

Explain why transaction mode suits Prisma Client while the Schema Engine needs one dedicated connection, and what Prisma 7 did to directUrl.

for a senior

Recognise the s0 prepared-statement error as migrations going through the pooler, and fix the URLs rather than the pooler.

for a principal

Decide where migrations run and with which credentials so the direct path to the database stays narrow and audited.

## Where PgBouncer sits **PgBouncer** is an external connection pooler. Clients connect to it as if it were PostgreSQL, and it hands their work to a small set of real server connections. In **transaction mode** a server connection is assigned to a client only for the duration of one transaction, then returned for someone else. That is what lets many application processes share a few database connections, and it is the mode the Prisma docs say PgBouncer must run in for Prisma Client to work reliably. ## Runtime traffic: Prisma Client through the pooler At runtime, Prisma Client sends queries through its driver adapter. With `PrismaPg`, you pass the pooled URL as `connectionString`. Three details matter: - The Prisma docs say Prisma uses prepared statements and ask you to set PgBouncer's `max_prepared_statements` to a value greater than zero so PgBouncer can handle them. - `PrismaPg` gives a statement a name, and so has node-postgres cache it, only when you pass a `statementNameGenerator` in the adapter options; the adapter's own documentation says that without one, prepared statements are not cached. - The `?pgbouncer=true` URL flag is documented for PgBouncer versions below 1.21.0. For 1.21.0 and later the docs recommend not setting it. ## Migrations: why the CLI needs a direct connection The **Prisma CLI** runs migrations through the **Schema Engine**. According to the Prisma docs, Prisma Migrate uses database transactions to check the current state of the database and the migrations table, and the Schema Engine is designed to use a single connection to the database. It does not support connection pooling with PgBouncer. Run `prisma migrate dev` or `prisma migrate deploy` against the pooled URL and you may see: `ERROR: prepared statement "s0" already exists` The fix is not a PgBouncer setting but a different URL: the CLI should bypass the pooler entirely. ## Two URLs in Prisma 7 | Variable | Points at | Used by | Set in | |---|---|---|---| | `DATABASE_URL` | PgBouncer | Prisma Client at runtime | `new PrismaPg({ connectionString })` | | `DIRECT_URL` | PostgreSQL directly | Prisma CLI: migrate, db push, db pull | `datasource.url` in `prisma.config.ts` | In Prisma 6 the schema's `datasource` block carried both `url` and `directUrl`, and CLI commands that needed a direct connection used the second. Prisma 7 removed `directUrl`; the single `datasource.url` in the config file is what CLI commands use. The upgrade guide is explicit: if you used `directUrl` for migrations, put that value in `url` in `prisma.config.ts`. ## A checklist 1. Run PgBouncer in transaction mode with `max_prepared_statements` greater than `0`. 2. Put the pooled URL in `DATABASE_URL` and pass it to `PrismaPg`. 3. Put the direct URL in `DIRECT_URL` and use `env("DIRECT_URL")` for `datasource.url` in `prisma.config.ts`. 4. Run `prisma migrate deploy` from a place that can reach the database directly, such as a release step, not through the pooler. 5. Add `?pgbouncer=true` only if your PgBouncer is older than 1.21.0. ## Verifying the setup After wiring the two URLs, check each path once: - Run `prisma migrate status` from the release environment. It reads `datasource.url`, so success confirms the CLI reaches the database directly. - Start the application and run a query. It goes through `PrismaPg`, so success confirms the pooled path. - Look at PgBouncer's client list while the application runs: the application's connections should appear there, and the CLI's should not. - Make sure the release environment can actually reach the direct host; poolers are often the only endpoint exposed to application networks, and migrations then need their own network route. ## What is Prisma-specific here The general behaviour of PgBouncer's pool modes, and which session state survives between transactions, belongs to PgBouncer itself. What is specific to Prisma comes down to three facts: - Prisma Client works through PgBouncer in transaction mode, and a Prisma interactive transaction stays on one server connection because it is one database transaction. - The Schema Engine behind Prisma Migrate does not support pooling through PgBouncer and needs a direct connection. - In Prisma 7 the config file's single `datasource.url` is how you keep the CLI on that direct connection while the adapter stays on the pooler.

  • What replaced datasource.directUrl in Prisma 7?
    Nothing with that name: Prisma 7 removed `directUrl` and made `datasource.url` in `prisma.config.ts` the URL every CLI command uses. If you previously kept the pooled URL in `url` and a direct one in `directUrl`, the upgrade is to put the direct URL in the config's `url` and pass the pooled URL to the driver adapter.
  • Do you still add ?pgbouncer=true to the connection string?
    Only for PgBouncer older than 1.21.0. For 1.21.0 and later the Prisma docs recommend not setting it, and ask instead that PgBouncer's `max_prepared_statements` be greater than zero so prepared statements work through the pooler.

  • Prisma Client needs PgBouncer in session mode, not transaction mode.
  • prisma migrate deploy is safe through the pooled URL because it is a single command.
  • Prisma 7 reads datasource.directUrl from prisma.config.ts for migrations.
  • Every PgBouncer version needs ?pgbouncer=true in Prisma's connection string.
  • The application should use the direct URL and the CLI the pooled one.

Assumes Prisma 7, where datasource.directUrl was removed and datasource.url in prisma.config.ts is the URL every CLI command uses.