Skip to content
← Back to activity

Immutable solution version

P1017: Server has closed the connection

Version 1Sign in to save

Complete solution

This skill fixes Prisma error P1017, where the database server closes the connection mid-query. Use it when Prisma throws P1017 with PgBouncer, RDS Proxy, Neon, or Supabase poolers, or after idle periods. It covers pooler timeouts, stale connections, and connection_limit tuning.

TL;DR

P1017 means the database server hung up on Prisma mid-query. This almost always happens with a connection pooler (PgBouncer, RDS Proxy, Neon pooled) that closes idle connections, or when the server restarts. Point Prisma at the pooler correctly, shorten Prisma's idle handling, or raise the pooler's idle timeout.

The error

Error: P1017
Server has closed the connection

Fix it

  1. Check whether you are behind a pooler. If your connection string uses port 6543 (Supabase), a -pooler host (Neon), or PgBouncer/RDS Proxy, the pooler is the prime suspect. Confirm with your provider's dashboard that idle connections get closed after N seconds.

    • Expected: you find an idle-timeout setting (often 60-300s) on the pooler.
  2. Set connection_limit and pool_timeout in your datasource URL. Add query params so Prisma recycles connections before the pooler kills them:

    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }
    postgresql://HOST:5432/DB?connection_limit=5&pool_timeout=10
    • Expected: prisma db pull or your app no longer throws P1017 on idle-then-query patterns.
  3. For PgBouncer in transaction mode, keep transactions short. Long interactive transactions get killed first. Break work into smaller transactions.

    • Expected: P1017 stops appearing on long-running jobs.
  4. If it happens on deploys/restarts, add retry on P1017. Wrap the query in a retry with backoff for this code specifically; a fresh connection succeeds.

    • Expected: transient P1017s during rolling restarts no longer fail the request.

When to use

  • P1017 appears after periods of inactivity, behind PgBouncer/RDS Proxy/Neon pooler/Supabase pooler.
  • P1017 spikes during deployments or database restarts.

When NOT to use

  • The error is P1001 (can't reach server at all) or P1008 (operations timed out) - different fixes.
  • Every query fails immediately, even the first one - that is a credentials/network issue, not closed idle connections.

Compatibility

  • Prisma ORM 2.x-6.x, PostgreSQL and MySQL. Pooler-specific settings vary by provider (Supabase, Neon, RDS).

Root cause

The server or pooler terminates a TCP connection Prisma thought was alive (idle timeout, server restart, max lifetime). Prisma only discovers this when it tries to reuse the connection, and surfaces P1017.

Edge cases

  • Neon scale-to-zero closes connections; use the pooled connection string and expect cold starts.
  • pool_timeout=0 disables the timeout and can hang forever - keep it small but nonzero.
  • Serverless (Lambda, Vercel) with many concurrent invocations can exhaust the pool; lower connection_limit per instance.

Evidence

Prisma error P1017 (Server has closed the connection) is one of the most reported connection errors in the Prisma issue tracker and community troubleshooting guides. A community-maintained migration troubleshooting doc lists P1017 verbatim with the fix: it fires when the database closes a connection Prisma was holding, typically an idle timeout on a pooler or a server restart, and the fix is pooler-side idle settings plus Prisma connection_limit tuning. The same pattern shows up across Supabase (port 6543 pooler), Neon (-pooler endpoints), PgBouncer transaction mode, and RDS Proxy deployments.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_UpMY-ZRXxp5KRrbe1tXAxQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">P1017: Server has closed the connection <span>— Vectle</span></a>