question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"FATAL: sorry, too many clients already" postgres error in develop mode on

See original GitHub issue

Bug description

I use prisma inside an api-route of https://nextjs.org/ (this is by the way an awesome setup) for a graphql-api. After some time you will get this error:


Error in connector: Error querying the database: db error: FATAL: sorry, too many clients already
    at PrismaClientFetcher.request

I guess the hot-reloading or refreshing of nextjs might mess with the connection-pool of prisma. I verified that the prisma-client that is used in the route is a singleton:

// this is imported in my api route
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

export default prisma;

How to reproduce

Steps to reproduce the behavior:

  1. create a file pages/api/graphql.ts inside that, use prisma client
  2. it probably needs some code changes that result in a rebuilding of this file /api/graphql.ts or its imports
  3. at some point you should get the error

Expected behavior

should not throw this error i guess?

Prisma information

Environment & setup

  • OS: macOS
  • Database: [PostgreSQL
  • Prisma version: prisma2@2.0.0-preview024, binary version: 377df4fe30aa992f13f1ba152cf83d5770bdbc85
  • Node.js version: v12.13.1

EDIT: Solution

as many still comment on this thead, I though it would be good to pin the solution here.

This issue happens because many platforms as nextjs (and probably nestjs as well) do hot reload of parts of your code. Usually you initialize the PrismaClient once in your application (as a singleton). But hot reload results in multiple initializations of this PrismaClient

so the solution is to kindof “cache” the client in a global variable. See this comment: https://github.com/prisma/prisma/issues/1983#issuecomment-620621213

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:60 (16 by maintainers)

github_iconTop GitHub Comments

45reactions
pantharshit00commented, Apr 28, 2020

@cimchd You should indeed refactor it so that the code is using a single instance of the Prisma client. Every new call to new PrismaClient() will spin up a new connection pool.

@macrozone You can try the following code to make sure next js hot reloading doesn’t create a new prisma instance every hot reload:

import { PrismaClient } from "@prisma/client"

let prisma

if (process.env.NODE_ENV === "production") {
  prisma = new PrismaClient()
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient()
  }

  prisma = global.prisma
}

export default prisma

This snippet was originally posted here: https://github.com/prisma/prisma-client-js/issues/228#issuecomment-618433162

14reactions
giraffesyocommented, Sep 3, 2020

For those having this issue, I solved it by creating a singleton:

import { PrismaClient } from '@prisma/client'

class DBClient {
  public prisma: PrismaClient
  private static instance: DBClient
  private constructor() {
    this.prisma = new PrismaClient()
  }

  public static getInstance = () => {
    if (!DBClient.instance) {
      DBClient.instance = new DBClient()
    }
    return DBClient.instance
  }
}

export default DBClient

Then wherever I used the client I did:

import DBClient from '../../database/client'
const prisma = DBClient.getInstance().prisma

You can then use it like normal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

org.postgresql.util.PSQLException: FATAL: sorry, too many ...
An explanation of the following error: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already. Summary: You opened up more than the ...
Read more >
psql: FATAL: sorry, too many clients already
This seems to be client programming specific problem. You won't be able to fix this by e.g. raising "max_connections" parameter.
Read more >
[ RESOLVED ] PostgreSQL : PSQLException: FATAL: sorry ...
[ RESOLVED ] PostgreSQL : PSQLException: FATAL: sorry, too many clients already · Exception · Cause. The application is trying to establish more ......
Read more >
How to resolve PostgreSQL 'FATAL: sorry, too many clients ...
PSQLException: FATAL: sorry, too many clients already. The error it's pretty much self-explanatory, and this is only the symptom. There is a ...
Read more >
Limits - Azure Database for PostgreSQL - Single Server
When connections exceed the limit, you may receive the following error: FATAL: sorry, too many clients already. Important.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found