Skip to content
Latchkey

Prisma "P1017: Server has closed the connection" in CI

P1017 means the database closed a connection Prisma was using. In CI this comes from a service that restarted, an idle-timeout on a pooler, or the DB being killed before the step finished.

What this error means

A migrate or query step fails partway with "P1017: Server has closed the connection.", sometimes only under load or on slower runners.

Prisma
Error: P1017: Server has closed the connection.

Common causes

The database service restarted or was still stabilizing

A service container that restarts (or is under memory pressure) drops in-flight connections, surfacing as P1017.

A pooler closed an idle or capped connection

A connection pooler (PgBouncer) or the server closed a connection that exceeded an idle or lifetime limit.

How to fix it

Ensure the database is stable before use

  1. Wait for the health check to pass before running Prisma.
  2. Give the service enough memory so it does not restart under load.
  3. Retry the operation once if the drop is transient.
.github/workflows/ci.yml
services:
  postgres:
    image: postgres:16
    options: >-
      --health-cmd="pg_isready" --health-interval=5s --health-retries=10

Tune connection lifetime with a pooler

When routing through a pooler, keep connection limits and lifetimes consistent so Prisma is not handed a connection about to be closed.

Terminal
DATABASE_URL=postgresql://user:pass@pooler:6432/app?pgbouncer=true&connection_limit=5

How to prevent it

  • Wait for a healthy database before running Prisma.
  • Provision enough resources so the service does not restart.
  • Align pool limits and lifetimes when using a pooler.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →