PostgresError: sorry, too many clients already

import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
import * as schema from './schema';

// for migrations
const migrationClient = postgres("postgres://root@localhost:5432/cafekampung", { max: 1 });
migrate(drizzle(migrationClient), {
migrationsFolder: "drizzle",
migrationsTable: "drizzle_migrations",
migrationsSchema: "public",
});

// for query purposes
const queryClient = postgres("postgres://root@localhost:5432/cafekampung");
export const db = drizzle(queryClient, { schema });
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
import * as schema from './schema';

// for migrations
const migrationClient = postgres("postgres://root@localhost:5432/cafekampung", { max: 1 });
migrate(drizzle(migrationClient), {
migrationsFolder: "drizzle",
migrationsTable: "drizzle_migrations",
migrationsSchema: "public",
});

// for query purposes
const queryClient = postgres("postgres://root@localhost:5432/cafekampung");
export const db = drizzle(queryClient, { schema });
this is the client
import { db } from "@/db";
import { restaurants } from "@/db/schema";
import { eq } from "drizzle-orm";

export function getAllMenu() {
return db.query.restaurants.findFirst({
where: eq(restaurants.id, 2),
with: {
menuItems: true
}
})
}
import { db } from "@/db";
import { restaurants } from "@/db/schema";
import { eq } from "drizzle-orm";

export function getAllMenu() {
return db.query.restaurants.findFirst({
where: eq(restaurants.id, 2),
with: {
menuItems: true
}
})
}
I'm calling it like this, suddenly it says
PostgresError: sorry, too many clients already
at Socket.emit (node:events:517:28)
digest: "3057589683"
PostgresError: sorry, too many clients already
at Socket.emit (node:events:517:28)
digest: "3057589683"
3 Replies
farhan
farhanOP6mo ago
@moderator need help it happens again
rphlmr ⚡
rphlmr ⚡6mo ago
👋 is it next js? If so, it is probably due to hot reload that recreates again and again a Drizzle client. The solution for this is to persist (for dev env only) the client in globalThis.
farhan
farhanOP6mo ago
yes nextjs, okay thank you for your help~
Want results from more Discord servers?
Add your server