Skip to content
Latchkey

Prisma P1001 "Can't reach database server" in CI

Prisma tried to connect to the database named in DATABASE_URL and the TCP connection never established. In CI this almost always means the database service container has not finished starting, or the host/port in the URL does not match it.

What this error means

prisma migrate deploy or prisma migrate dev fails with "P1001: Can't reach database server at localhost:5432" early in the job, before any SQL is sent.

prisma output
Error: P1001: Can't reach database server at `localhost`:`5432`

Please make sure your database server is running at `localhost`:`5432`.

Common causes

The database service container is not ready yet

The migrate step starts before Postgres inside the service container has finished initializing, so the connection is refused even though the container exists.

Wrong host, port, or networking in DATABASE_URL

The URL points at localhost when the database is reachable only by its service name, or the port does not match the mapped port.

How to fix it

Wait for the database to accept connections

Add a health check or poll with pg_isready before running migrate so the step waits until the server is up.

.github/workflows/ci.yml
services:
  postgres:
    image: postgres:16
    env: { POSTGRES_PASSWORD: postgres }
    ports: ['5432:5432']
    options: >-
      --health-cmd "pg_isready -U postgres"
      --health-interval 5s --health-retries 10

Point DATABASE_URL at the right host

  1. When the job runs directly on the runner, use localhost and the mapped port.
  2. When the job runs inside a container on the same network, use the service name as the host.
  3. Confirm the port matches the one the database actually listens on.
Terminal
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/app

How to prevent it

  • Gate migrate behind a database health check, not a fixed sleep.
  • Keep the host/port in DATABASE_URL consistent with how the job is run.
  • Fail fast with pg_isready so connection problems are obvious.

Related guides

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