Skip to content

adapter-pg doesn't properly handle DB connection errors #27626

Closed

Description

@yusufkandemir

Bug description

The adapter / Prisma client doesn't throw an error at await prisma.$connect() if it can't connect to the server. You only get an error when trying to run a query. You would get a PrismaClientKnownRequestError with code set to ECONNREFUSED/ENOTFOUND as opposed to getting known error codes such as P1000, P1001, etc.

It seems to create the pool instance, but don't call .connect() off of it, so it doesn't fail on incorrect configurations:
https://github.com/prisma/prisma/blob/c6936205a0cfb0d56887733f84e061197efba37c/packages/adapter-pg/src/pg.ts#L221-L223

This behavior is different compared to using it without driver adapters. It also differs from some other adapters such as MSSQL:

https://github.com/prisma/prisma/blob/c6936205a0cfb0d56887733f84e061197efba37c/packages/adapter-mssql/src/mssql.ts#L159-L163

Severity

⚠️ Major: Breaks core functionality (e.g., migrations fail)

Reproduction

  • Create a new project with Prisma
  • Enable queryCompiler and driverAdapters preview features
  • Install @prisma/adapter-pg and @types/pg(optional, for typechecking in adapter params)
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "src/@generated/prisma/client";

const adapter = new PrismaPg(
  {
    host: 'non-existing-host', // <-- not existing, so $connect() should logically fail
    port: 5432,
    user: 'postgres',
    password: 'pass',
    database: 'postgres',
  },
  {
    schema: 'public',
  },
);

const prisma = new PrismaClient({
  adapter,
});

console.log('Connecting to DB');
await prisma.$connect();
console.log('Successfully to DB'); // <-- You will see this being logged, whereas it should have thrown above

Expected vs. Actual Behavior

Expected: await prisma.$connect() to throw an error with P1000/P1001/etc. on DB connection errors.
Actual: await prisma.$connect() doesn't throw any errors on DB connection errors. If executing queries in that state, the queryies throw an error with code set to ECONNREFUSED/ENOTFOUND instead of P1000/P1001/etc.

Frequency

Consistently reproducible

Does this occur in development or production?

Both development and production

Is this a regression?

Yes, relative to pre driver adapters behavior, if that counts.

Workaround

Moving away from driver adapters.
Or messing with the internal stuff, like doing await (await adapter.connect()).client.connect() to trigger a connection.
Or creating a pg pool instance to manually check for connection.

Prisma Schema & Queries

N/A

Prisma Config

No response

Logs & Debug Info

No response

Environment & Setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v22.16.0

Prisma Version

prisma                : 6.11.1
@prisma/client        : 6.11.1
Computed binaryTarget : darwin-arm64
Operating System      : darwin
Architecture          : arm64
Node.js               : v22.16.0
TypeScript            : 5.8.3
Query Compiler        : enabled
PSL                   : @prisma/prisma-schema-wasm 6.11.1-1.f40f79ec31188888a2e33acda0ecc8fd10a853a9
Schema Engine         : schema-engine-cli f40f79ec31188888a2e33acda0ecc8fd10a853a9 (at ../../node_modules/.pnpm/@[email protected]/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Default Engines Hash  : f40f79ec31188888a2e33acda0ecc8fd10a853a9
Studio                : 0.511.0
Preview Features      : driverAdapters, queryCompiler

Activity

  1. barryhagan commented on Jul 13, 2025

    @barryhagan
    Contributor

    There is an entire class of issues with the handling of database connections in @prisma/adapter-pg. While errors returned from the postgres server are handled and converted into DriverAdapterErrorObject, errors from the underlying net.Socket are treated as invalid Prisma invocation query errors rather than a driver error.

    If you haven't successfully connected to the server yet (ENOTFOUND, ECONNREFUSED, etc.), the error is emitted from the socket and thrown here because it doesn't have a severity property. The code here needs to be updated to identify and handle socket errors.

    That won't fix the original ask in this issue, because connect() is lazy until you actually make a query, but it will improve the confusing error handling once a db connection is attempted.

  2. added a commit that references this issue on Jul 23, 2025
    0b69b09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions