TypeScript
---
title: Postgres
description: SQL.Postgres turns a connection string into an @effect/sql-pg client — tagged-template queries with typed errors, one pool per execution.
---
import PackageManagers from "../../../../components/PackageManagers.astro";
export const deps = `@alchemy.run/sql @effect/sql-pg`;
`SQL.Postgres` provides tagged-template queries over a Postgres connection. Queries are Effects with typed `SqlError` failures, interruption, and tracing.
Install `@alchemy.run/sql` and the Effect Postgres driver:
<PackageManagers add={deps} />
## Connect
```typescript
import * as SQL from "@alchemy.run/sql/Postgres";
import * as Effect from "effect/Effect";
import type * as Redacted from "effect/Redacted";
export const makeQueries = <E, R>(
connectionString:
| Redacted.Redacted<string>
| Effect.Effect<Redacted.Redacted<string>, E, R>,
) =>
Effect.gen(function* () {
const sql = yield* SQL.Postgres({ url: connectionString });
return {
listUsers: () => sql<{ id: number; name: string }>`SELECT id, name FROM users`,
};
});
```
`url` accepts a redacted URL or an Effect resolving one, such as `Config.Redacted("DATABASE_URL")` or a runtime binding's `connectionString`. Other `@effect/sql-pg` pool options pass through. Call queries within a request or an explicit `Effect.scoped` block: the pool opens lazily and closes with that [scope](/sql/effect-sql/lifecycle).
## Queries
Interpolated values are parameters, never string concatenation:
```typescript
const user = yield* sql`SELECT * FROM users WHERE id = ${id}`;
yield* sql`INSERT INTO users ${sql.insert({ name, email })}`;
const rows = yield* sql`
SELECT * FROM users WHERE id IN ${sql.in(ids)}
`;
```
Rows are plain objects; supply a row type with `sql<Row>`. The
[`effect/sql/Statement`](https://effect.website) API also provides
fragments, `sql.csv`, `sql.and`, and identifier escaping.
## Errors
Failures surface as `SqlError` in the typed error channel:
```typescript
const users = yield* sql`SELECT * FROM users`.pipe(
Effect.catchTag("SqlError", (e) =>
Effect.succeed([]).pipe(Effect.tap(() => Effect.logWarning(e))),
),
);
```
## Transactions
Wrap a group of queries in `sql.withTransaction` — the whole effect
commits or rolls back together:
```typescript
yield* sql.withTransaction(
Effect.gen(function* () {
yield* sql`UPDATE accounts SET balance = balance - ${amount} WHERE id = ${from}`;
yield* sql`UPDATE accounts SET balance = balance + ${amount} WHERE id = ${to}`;
}),
);
```
## Provide as a service
Depend on the generic `SqlClient` tag, then provide the database layer:
```typescript
import * as SqlClient from "effect/sql/SqlClient";
const makeUsers = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
return {
find: (id: number) => sql<User>`SELECT * FROM users WHERE id = ${id}`,
};
});
const users = yield* makeUsers.pipe(
Effect.provide(SQL.PostgresLayer({ url: connectionString })),
);
```
The layer provides `SqlClient.SqlClient` and `@effect/sql-pg`'s `PgClient` from one per-execution pool. Other drivers can satisfy the generic service, but changing engines still requires compatible SQL and transaction semantics.
## Provider setup
Choose a database and deployment guide in [SQL databases](/sql/databases); the client above only needs a connection URL. [Hyperdrive](/cloudflare/data/hyperdrive#bind-hyperdrive-to-your-worker) and [Fly Managed Postgres](/fly/data/postgres) cover runtime-specific bindings.
## Where next
Read [Migrations](/sql/effect-sql/migrations) to apply committed SQL files, or [Drizzle on Postgres](/sql/drizzle/postgres) for typed schemas over the same driver. [Connection lifecycle](/sql/effect-sql/lifecycle) covers per-request cleanup.