---
title: "PgBouncer and PostgreSQL in 2026: Prisma, Django, Rails, migrations"
url: https://while.sh/en/guides/postgresql-pgbouncer/
author: "Aloïs Oddon, co-founder of Nextmap"
updated: 2026-11-19
---

# PostgreSQL behind PgBouncer: Prisma, Django, Rails and your migrations (2026)

Your PostgreSQL database on while goes through PgBouncer, to handle hundreds of clients at once. Your AI knows what that changes for your code, and sets it up for you.

> **The short version.** Your while database goes through PgBouncer in transaction mode. Here is what your AI sets up for Prisma, Django or Rails, and where migrations run. What matters, like deleting a database, waits for a person's approval.

## Prerequisites

- An app deployed on while, with its PostgreSQL database.
- Your AI, connected to while over MCP.

## Step 1: Ask for a check

> You, to your AI: Check that our code works behind PgBouncer.

Your AI reviews your code and your database settings, and fixes anything that assumes it keeps the same database connection from one query to the next.

## Step 2: What needs a session runs separately

Anything that needs a full session, like `pg_restore` or a migration that runs `SET` commands, runs in a job, which your AI also starts on request. The job receives the database's direct address in `DB_MAIN_DIRECT_URL`.

## What your AI does

In transaction mode, each transaction can get a different connection. Prepared statements are tracked, so Prisma and node-postgres work with no configuration. A session `SET`, an advisory lock or a server-side cursor, however, doesn't carry over from one transaction to the next (source 1). To configure the session, your AI uses `SET LOCAL`, inside a transaction:

```js titre="server.js"
const client = await db.connect();
try {
  await client.query('BEGIN');
  await client.query("SET LOCAL statement_timeout = '5s'");
  await client.query('SELECT count(*) FROM commandes');
  await client.query('COMMIT');
} catch (e) {
  await client.query('ROLLBACK');
  throw e;
} finally {
  client.release();
}
```


<figure class="illus"><img src="/illustrations/guide-fonction-garde-1200.webp" srcset="/illustrations/guide-fonction-garde-768.webp 768w, /illustrations/guide-fonction-garde-1200.webp 1200w" sizes="(min-width: 48rem) 46rem, 100vw" width="1536" height="1024" loading="lazy" decoding="async" alt="PostgreSQL behind PgBouncer with while: credentials kept out of the AI's reach, every call logged in the history, a person's approval for what matters and a daily backup, in France" /><figcaption>Around every app, while keeps the credentials, the history, the approvals and the backups.</figcaption></figure>

## Set up Django

> You, to your AI: Set up Django for PgBouncer.

Your AI turns off server-side cursors:

```python titre="settings.py"
DATABASES["default"]["DISABLE_SERVER_SIDE_CURSORS"] = True
```

## Set up Rails

> You, to your AI: Set up Rails for PgBouncer.

Your AI turns off the migrations' advisory locks:

```yaml titre="config/database.yml"
production:
  primary:
    url: <%= ENV["DATABASE_URL"] %>
    advisory_locks: false
```

## Keep Prisma as is

> You, to your AI: Use Prisma for the migrations.

Your AI leaves Prisma on `DATABASE_URL`, migrations included, so there is nothing to change. It doesn't declare a `directUrl`, which Prisma would then demand at startup with error P1012, since the direct address is given only to jobs.

## Find the slow queries

> You, to your AI: Which queries are slowing down the database?

Your AI reads your database's most frequent and slowest queries, as recorded by pg_stat_statements.

## Next steps

- [Deploy a Node.js API and its PostgreSQL database](/en/guides/deploy-node-api-postgresql/)
- [Deploy a Django app](/en/guides/deploy-django/)
- [Restore your database to a precise moment](/en/guides/restore-postgresql/)

## Sources

1. [PgBouncer, what each pooling mode supports](https://www.pgbouncer.org/features.html), 2026-11-02

## Questions

### Do I need ?pgbouncer=true for Prisma?

No: PgBouncer tracks prepared statements, and the DATABASE_URL address works as is.

### Can I connect without PgBouncer?

A job can: it receives the direct address in DB_MAIN_DIRECT_URL. Your app always goes through PgBouncer.

### How many clients at the same time?

It depends on the database size: 200 on S, the default size, 400 on M, 600 on L, 800 on XL.

### Where do my app and its data run?

In France, at Nextmap. The database is backed up every day, with a way back over 14 days, once a person approves.

### Does my AI see my credentials?

No. You paste them into while, the app uses them, and your AI never sees them. Every call is logged.
