Skip to content

Drizzle.Postgres reference

Source: packages/drizzle/src/Postgres.ts

Open a Drizzle/Postgres database from a connection URL using the drizzle-orm/effect-postgres integration.

Resolve it once at Construction phase and use it from any handler:

const db = yield* Drizzle.Postgres(hd.connectionString);
fetch: Effect.gen(function* () {
const rows = yield* db.select().from(users);
});

The database is a real EffectPgDatabase. Queries, subqueries, CTEs and sql fragments are built by drizzle’s own code, with no connection. Running a query opens the pool. The pool is opened at most once per execution — a Worker fetch/queue/scheduled event, a Durable Object call, a Workflow run, or a Lambda invocation — and reused by every query and task step in it. The connection string resolves then too, so deploy / plan-time invocations (where WorkerEnvironment isn’t provided) never connect.

The pool belongs to the execution’s Scope and closes when the request / run settles. Wrapping queries in a nested Effect.scoped gives that block its own pool, closed when the block ends.

@effect/sql-pg client options are passed via config.client. Behind a transaction-mode pooler such as Hyperdrive, turn off named prepared statements, since a later query can reach a backend session that never prepared them:

const db = yield* Drizzle.Postgres(hd.connectionString, {
client: { prepare: false },
});