> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kodus.io/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL behind a connection pooler

> Run Kodus with PgBouncer or another pooler in front of PostgreSQL, including highly available clusters.

Kodus works with PostgreSQL behind a connection pooler such as PgBouncer, in
**session or transaction** pooling mode. CI runs the Kodus coordination
locks and the event bridge between api and worker through PgBouncer 1.26 in
transaction mode on every change.

This page covers what to configure on the pooler, the two connections Kodus
uses, and how to check the result.

## Two connections

| Connection | Variables | Used for | May point at the pooler? |
| - | - | - | - |
| Pooled | `API_PG_DB_HOST`, `API_PG_DB_PORT` (or `DATABASE_URL`) | Every query the api, worker and webhooks run | Yes |
| Direct | `API_PG_DB_DIRECT_HOST`, `API_PG_DB_DIRECT_PORT` (or `DATABASE_DIRECT_URL`) | Database migrations, and the LISTEN connection that carries live events from the worker to the api | **No**: it must reach PostgreSQL itself |

The direct connection reuses `API_PG_DB_USERNAME`, `API_PG_DB_PASSWORD` and
`API_PG_DB_DATABASE`, so usually only the host changes. When it is not set,
Kodus uses the pooled connection for everything, as it does without a pooler.

Why the direct connection matters in transaction mode:

* **Live events.** LISTEN needs a session that stays open. Behind a
  transaction-mode pooler it connects and then never receives anything.
  Review status in the UI and rule sync after a merge then arrive through
  polling, up to 10 seconds late.
* **Migrations.** Some migrations build indexes with `CREATE INDEX
  CONCURRENTLY` after changing the statement timeout of their session. In
  transaction mode the two statements can land on different server sessions.

## Configure PgBouncer

```ini theme={null}
[pgbouncer]
pool_mode = transaction
; Kodus sets these two limits on each connection (PgBouncer 1.20 or later).
; Without this line PgBouncer refuses every Kodus connection with
; "unsupported startup parameter: statement_timeout".
track_extra_parameters = statement_timeout,idle_in_transaction_session_timeout
; Each Kodus process opens its own pool (api 30, webhooks 30, worker 60
; by default, plus up to 6 for internal events). Budget for every replica.
max_client_conn = 1000
default_pool_size = 40
```

If your pooler cannot track these parameters (an older PgBouncer, or a
managed pooler you cannot configure), turn them off on the Kodus side and
set equivalent limits on the database role instead:

```bash theme={null}
DB_STATEMENT_TIMEOUT_MS=0
DB_IDLE_IN_TRANSACTION_TIMEOUT_MS=0
```

```sql theme={null}
ALTER ROLE kodusdev IN DATABASE kodus_db SET statement_timeout = '30s';
ALTER ROLE kodusdev IN DATABASE kodus_db SET idle_in_transaction_session_timeout = '60s';
```

## Configure Kodus

Set these on **api, worker, worker-analytics, webhooks** and on whatever
runs the migrations (`RUN_MIGRATIONS=true`):

```bash theme={null}
API_PG_DB_HOST=pgbouncer.internal          # the pooler
API_PG_DB_PORT=6432
API_PG_DB_DIRECT_HOST=postgres-primary.internal   # PostgreSQL itself
API_PG_DB_DIRECT_PORT=5432
```

With connection strings instead:

```bash theme={null}
DATABASE_URL=postgres://kodusdev:...@pgbouncer.internal:6432/kodus_db
DATABASE_DIRECT_URL=postgres://kodusdev:...@postgres-primary.internal:5432/kodus_db
```

## Turn it on with the installer

The `kodus-installer` wires the pooler for each way of running Kodus, and
fills in the settings above for you.

Setting the direct connection also moves Kodus's coordination locks from
session locks to rows in the database. Pods still running without it do not
see the new locks, so turn the pooler on with the old pods stopped: run
`docker compose down` first, or scale the api, worker and webhooks
Deployments to 0 before the `helm upgrade`.

### Docker Compose

The `pgbouncer` profile starts PgBouncer 1.26 in transaction mode in front of
the bundled PostgreSQL, with `track_extra_parameters` already set. Point Kodus
at it in `.env`, keeping PostgreSQL as the direct connection:

```bash theme={null}
API_PG_DB_HOST=db_kodus_pgbouncer
API_PG_DB_PORT=5432
API_PG_DB_DIRECT_HOST=db_kodus_postgres
```

```bash theme={null}
COMPOSE_PROFILES=pgbouncer docker compose up -d
```

To use your own pooler instead, set the four variables from
Configure Kodus above to your hosts and leave the profile off.

### Helm, with the CloudNativePG operator

With `postgres.mode: operator`, `postgres.operator.pooler.enabled: true` adds a
CloudNativePG `Pooler` in front of `<release>-postgres-rw`. The app connects to
`<release>-postgres-pooler-rw`; migrations and LISTEN stay on
`<release>-postgres-rw`. The chart always adds `track_extra_parameters`, on top
of any `parameters` you set.

```yaml theme={null}
postgres:
  mode: operator
  operator:
    image: <cnpg-image-with-pgvector>
    pooler:
      enabled: true
      poolMode: transaction   # or session
      instances: 2
      parameters:
        max_client_conn: "1000"
        default_pool_size: "40"
```

`poolMode` takes `transaction` (default) or `session`. Size
`max_client_conn` for every api, worker and webhooks replica at
`autoscaling.maxReplicas`.

### Helm, with your own pooler

With `postgres.mode: external`, point `host`/`port` at the pooler and
`directHost`/`directPort` at PostgreSQL. When a connection string is needed for
the direct connection, use `directUrlKey` (next section).

```yaml theme={null}
postgres:
  mode: external
  external:
    host: pgbouncer.internal
    port: 6432
    directHost: postgres-primary.internal
    directPort: 5432
    existingSecret: kodus-postgres
```

`postgres.mode: bundled` has no pooler. Use `operator` or `external` to run
Kodus behind one.

## Your own PgBouncer or managed pooler

If PostgreSQL and its pooler are already running on your side, point Kodus
at them, with Docker Compose or the Helm chart alike. Check your setup
against this table:

| Your setup | What to do |
| - | - |
| Pool mode `session` | Works without a direct connection. Each Kodus connection holds a server connection while it is open, so size the pool for every Kodus pool, on every replica |
| Pool mode `transaction` | Set the direct connection (above) |
| Pool mode `statement` | Not supported: Kodus runs transactions with several statements |
| The pooler exposes the database under another name, or Kodus logs in to it with another user | The host form of the direct connection reuses `API_PG_DB_DATABASE` and `API_PG_DB_USERNAME`, which do not exist on PostgreSQL. Set `DATABASE_DIRECT_URL` with the real ones |
| Kodus can reach the pooler but not PostgreSQL | Add a session-mode entry on the same PgBouncer and use it as the direct connection (below) |
| PgBouncer older than 1.20, or a managed pooler where you cannot set `track_extra_parameters` | Set the two timeouts to `0` and move them to the database role (above) |
| TLS differs between the pooler and PostgreSQL | Give the direct connection its own `sslmode` in `DATABASE_DIRECT_URL` (below) |

**Only the pooler is reachable.** PgBouncer can run one database entry in
session mode next to the transaction-mode one. Kodus uses it for migrations
and for one long-lived LISTEN connection per process, so a small pool is
enough:

```ini theme={null}
[databases]
kodus = host=postgres-primary.internal dbname=kodus_db pool_mode=transaction
kodus_session = host=postgres-primary.internal dbname=kodus_db pool_mode=session
```

```bash theme={null}
API_PG_DB_HOST=pgbouncer.internal
API_PG_DB_PORT=6432
API_PG_DB_DATABASE=kodus
DATABASE_DIRECT_URL=postgres://kodusdev:...@pgbouncer.internal:6432/kodus_session
```

**TLS.** `API_DATABASE_DISABLE_SSL` applies to every connection, so when the
pooler speaks plain TCP and PostgreSQL requires TLS (or the other way round),
keep the pooled settings as they are and put `sslmode` in
`DATABASE_DIRECT_URL`; the URL's `sslmode` wins for that connection. The
PostgreSQL driver in Kodus reads `sslmode=require` and `sslmode=prefer` as
`verify-full`, so the server certificate must validate (add your CA with
`NODE_EXTRA_CA_CERTS`). Use `sslmode=no-verify` to encrypt without checking
the certificate, and `sslmode=disable` for plain TCP.

**Helm chart.** In `external` mode, put the direct connection string in the
Secret named by `postgres.external.existingSecret` and set
`postgres.external.directUrlKey` to its key. Every Kodus pod and the
migration Job receive it as `DATABASE_DIRECT_URL`.

## Highly available PostgreSQL

Kodus connects to one host per connection; it does not take a list of hosts
or `target_session_attrs`. Point both connections at endpoints that always
lead to the **current primary**: a virtual IP, HAProxy with Patroni health
checks, the `-rw` service of CloudNativePG, or a PgBouncer that follows the
primary. Kodus writes from every component, so read replicas are not used.

During a failover, queries in flight fail and the pools reconnect on the
next request. Behind a pooler, coordination locks are rows in the database,
so they survive a failover with the data; without one they are session locks,
freed when the sessions to the old primary drop.

## Check the setup

Run the doctor from the `kodus-installer` directory: `./scripts/doctor.sh`,
or `./scripts/doctor-k8s.sh -n <namespace>` on Kubernetes (see
[Troubleshooting](https://docs.kodus.io/en/how_to_deploy/deploy_kodus/troubleshooting.md)). Two lines
cover this page:

* `database.locks`: Kodus can take its coordination locks. A failure here
  usually means the migrations have not run.
* `database.listen`: live events between api and worker arrive. A warning
  here means the LISTEN connection goes through a transaction-mode pooler;
  set the direct connection.

The api also logs the result once it connects:

```text theme={null}
Cross-process LISTEN verified: notifications reach this process
```

or, when events do not arrive:

```text theme={null}
Cross-process LISTEN is connected but notifications never arrive. ...
```

## Settings reference

| Variable | Default | What it does |
| - | - | - |
| `API_PG_DB_DIRECT_HOST` | unset | PostgreSQL host for migrations and LISTEN. Unset means the pooled host |
| `API_PG_DB_DIRECT_PORT` | `5432` | Port for the direct host |
| `DATABASE_DIRECT_URL` | unset | Full direct connection string; wins over the host form |
| `DB_STATEMENT_TIMEOUT_MS` | `30000` | Longest a single statement may run. `0` stops Kodus from sending it |
| `DB_IDLE_IN_TRANSACTION_TIMEOUT_MS` | `60000` | Longest a transaction may sit idle. `0` stops Kodus from sending it |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.