Misc
Misc
DTDrizzle Team
Created by Misc on 3/28/2025 in #help
Consistently hitting max clients/connections on postgres
just testing things out, thought it might solve the issue ahahah
6 replies
DTDrizzle Team
Created by Misc on 3/28/2025 in #help
Consistently hitting max clients/connections on postgres
I was having this on dev as well due the HRM but i changed a bit the config and it helped a lot, just not sure why this would happen on prod tho.
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

declare global {
// eslint-disable-next-line no-var
var postgresSqlClient: ReturnType<typeof postgres> | undefined;
}

let postgresSqlClient;

const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
throw new Error("DATABASE_URL is not set in .env.local");
}

// Connection pool settings
const poolConfig = {
max: 20, // Below Supabase limit of 30
idle_timeout: 10, // 10 seconds
connect_timeout: 5, // Timeout after 5 seconds if connection cannot be established
max_lifetime: 60 * 30, // Connection lifetime of 30 minutes
};

if (process.env.NODE_ENV !== "production") {
if (!global.postgresSqlClient) {
global.postgresSqlClient = postgres(connectionString, {
...poolConfig,
prepare: false,
});
}
postgresSqlClient = global.postgresSqlClient;
} else {
postgresSqlClient = postgres(connectionString, {
...poolConfig,
prepare: false,
});
}

// Disable prefetch as it is not supported for "Transaction" pool mode
export const db = drizzle(postgresSqlClient, { schema });
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

declare global {
// eslint-disable-next-line no-var
var postgresSqlClient: ReturnType<typeof postgres> | undefined;
}

let postgresSqlClient;

const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
throw new Error("DATABASE_URL is not set in .env.local");
}

// Connection pool settings
const poolConfig = {
max: 20, // Below Supabase limit of 30
idle_timeout: 10, // 10 seconds
connect_timeout: 5, // Timeout after 5 seconds if connection cannot be established
max_lifetime: 60 * 30, // Connection lifetime of 30 minutes
};

if (process.env.NODE_ENV !== "production") {
if (!global.postgresSqlClient) {
global.postgresSqlClient = postgres(connectionString, {
...poolConfig,
prepare: false,
});
}
postgresSqlClient = global.postgresSqlClient;
} else {
postgresSqlClient = postgres(connectionString, {
...poolConfig,
prepare: false,
});
}

// Disable prefetch as it is not supported for "Transaction" pool mode
export const db = drizzle(postgresSqlClient, { schema });
6 replies
DTDrizzle Team
Created by cpboyce6 on 3/25/2025 in #help
Hitting connection limits using Pgbouncer
did you ever figure this out? im running into the same issue
2 replies