Skip to content

SQL.Postgres reference

Source: packages/sql/src/Postgres.ts

Open an @effect/sql-pg client (a connection pool) from a connection URL.

Accepts a plain Redacted URL or an Effect of one — e.g. Cloudflare.Hyperdrive.Connect(...) or Fly.ConnectPostgres(...)’s connectionString — and returns a PgClient (which implements the generic SqlClient interface). Resolve it once at Construction phase and use it from any handler:

import * as SQL from "@alchemy.run/sql/Postgres";
const hd = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive);
const sql = yield* SQL.Postgres({ url: hd.connectionString });
fetch: Effect.gen(function* () {
const users = yield* sql`SELECT * FROM users`;
});

The client is a real PgClient: sql`...`, sql.insert, sql.and and sql.json build and compile statements with @effect/sql-pg’s own code, with no connection. Running a statement 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 closes when the event settles. The connection URL resolves then too, so deploy / plan-time invocations never connect.