Skip to content
Latchkey

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

The database accepted Prisma’s connection and then dropped it partway through. The connection died underneath an in-flight operation - a transient server-side event, not a schema mistake.

What this error means

prisma migrate deploy connects successfully and then fails with P1017 partway through. Re-running the job frequently succeeds, the hallmark of a transient connection drop.

prisma output
Error: P1017: Server has closed the connection.

Common causes

Transient server restart or failover

A managed database restarting, failing over, or recycling a node closes existing connections. Prisma sees the socket close mid-operation and reports P1017.

Idle or statement timeout on the server

A server-side idle_in_transaction_session_timeout or connection-lifetime limit can terminate a connection that Prisma still holds.

Connection pooler dropping the link

A pooler (PgBouncer, RDS Proxy) in front of the database may close a backend connection, surfacing as a closed connection to Prisma.

How to fix it

Retry the migrate step

Because the drop is transient, a bounded retry usually clears it without any change.

Terminal
for i in 1 2 3; do
  npx prisma migrate deploy && break
  echo "retry $i after closed connection"; sleep 5
done

Point migrations at a direct connection

Run migrations against the direct database URL, not a transaction-mode pooler that may close backends.

schema.prisma
datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")   // used by migrate
}

Check server-side timeouts and load

  1. Look at the database logs around the failure for restarts, failovers, or timeout terminations.
  2. Raise or disable aggressive idle timeouts for the migration window if you control the server.
  3. Avoid running migrations against a database that is mid-deploy or under heavy load.

How to prevent it

  • Run migrations against a direct connection, not a transaction-mode pooler.
  • Wrap migrate in a bounded retry for transient drops.
  • Schedule migrations away from database restarts and peak load.

Frequently asked questions

What causes ""P1017: Server has closed the connection""?
A managed database restarting, failing over, or recycling a node closes existing connections. Prisma sees the socket close mid-operation and reports P1017.
How do I fix "P1017: Server has closed the connection"?
Because the drop is transient, a bounded retry usually clears it without any change.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card