I'm running Supabase Postgres locally with Drizzle.
In production, I'd use the connection pooler which would ensure I don't have too many concurrent connections.
I can't locally however and Supabase often gives this error:
PostgresError: remaining connection slots are reserved for non-replication superuser connections
PostgresError: remaining connection slots are reserved for non-replication superuser connections
.
I'm initiating the db as such:
import { drizzle } from "drizzle-orm/postgres-js"import postgres from "postgres"import * as schema from "../schemas/drizzleSchema"export default function db() { const connectionString = process.env.DATABASE_URL if (!connectionString) { throw new Error("LOCAL_DATABASE_URL is undefined") } const client = postgres(connectionString) return drizzle(client, { schema })}
import { drizzle } from "drizzle-orm/postgres-js"import postgres from "postgres"import * as schema from "../schemas/drizzleSchema"export default function db() { const connectionString = process.env.DATABASE_URL if (!connectionString) { throw new Error("LOCAL_DATABASE_URL is undefined") } const client = postgres(connectionString) return drizzle(client, { schema })}
And then in the necessary functions I import db and call it, like so:
const database = db()
const database = db()
.
I have many helper functions which each create a db connection for the query, for example: