Fly.Postgres reference
ConnectPostgres
Section titled “ConnectPostgres”Source:
src/Fly/ConnectPostgres.ts
Bind a Postgres cluster to a Fly Service and obtain
the Effect-native connection strings for Drizzle.Postgres /
SQL.Postgres. Alchemy transports the cluster’s connection URIs
automatically; callers do not configure connection-string env vars.
ConnectPostgres is the Context tag, the type, and the callable —
yield* Fly.ConnectPostgres(Db). Provide ConnectPostgresHttp.
ConnectPostgres: Examples
Section titled “ConnectPostgres: Examples”import * as Drizzle from "alchemy/Drizzle/Postgres";
const conn = yield* Fly.ConnectPostgres(Db);const db = yield* Drizzle.Postgres(conn.connectionString);
fetch: Effect.gen(function* () { const rows = yield* db.select().from(users);});ConnectPostgresHttp
Section titled “ConnectPostgresHttp”Source:
src/Fly/ConnectPostgresHttp.tsKind: Layer · Provides:Fly.ConnectPostgres
Implementation of ConnectPostgres. Provide it on the
Service Effect.
Registers the cluster attachment for Fly’s private network. Alchemy transports each cluster’s connection URIs through resource Outputs, so callers do not configure connection-string environment variables.
ConnectPostgresHttp: Provide the layer
Section titled “ConnectPostgresHttp: Provide the layer”Effect.gen(function* () { const conn = yield* Fly.ConnectPostgres(Db); const db = yield* Drizzle.Postgres(conn.connectionString);}).pipe(Effect.provide(Fly.ConnectPostgresHttp))Postgres
Section titled “Postgres”Source:
src/Fly/Postgres.ts
A Fly.Postgres is a Managed Postgres (MPG) cluster. It is billed.
Do not wrap unmanaged fly postgres.
Postgres: Create a cluster
Section titled “Postgres: Create a cluster”region is required. Alchemy generates a unique name unless you
pass one. Omit name in tests and CI.
const db = yield* Fly.Postgres("Db", { region: "iad",});Postgres: Plan
Section titled “Postgres: Plan”plan is the hardware size: basic, starter, launch,
scale, performance. Default is basic.
const db = yield* Fly.Postgres("Db", { region: "iad", plan: "starter",});Postgres: Region
Section titled “Postgres: Region”The cluster is regional. An App is global. Pass region
on the cluster, not on the App.
const db = yield* Fly.Postgres("Db", { region: "lhr",});Postgres: Volume size
Section titled “Postgres: Volume size”volumeSizeGb is the initial disk. Fly defaults to 10 GB.
const db = yield* Fly.Postgres("Db", { region: "iad", volumeSizeGb: 20,});Postgres: PostGIS
Section titled “Postgres: PostGIS”postgis: true enables PostGIS at create.
const db = yield* Fly.Postgres("Db", { region: "iad", postgis: true,});Postgres: Connect from a Service
Section titled “Postgres: Connect from a Service”Yield ConnectPostgres inside init. Provide
ConnectPostgresHttp. Pass conn.connectionString to
Drizzle.Postgres or SQL.Postgres.
import * as Drizzle from "alchemy/Drizzle/Postgres";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export default class Api extends Fly.Service<Api>()( "Api", { app: Site, main: import.meta.url, port: 3000 }, Effect.gen(function* () { const conn = yield* Fly.ConnectPostgres(Db); const db = yield* Drizzle.Postgres(conn.connectionString); return { fetch: Effect.gen(function* () { const rows = yield* db.execute("select 1 as ok"); return HttpServerResponse.json({ rows }); }), }; }).pipe(Effect.provide(Fly.ConnectPostgresHttp)),) {}Postgres: Migrations
Section titled “Postgres: Migrations”Pass a directory, { dir, table? }, or a Drizzle.Schema resource.
Alchemy applies pending files on deploy over the direct URI.
Directory
const db = yield* Fly.Postgres("Db", { region: "iad", migrations: "./migrations",});Drizzle.Schema
const schema = yield* Drizzle.Schema("app-schema", { schema: "./src/schema.ts", out: "./migrations",});
const db = yield* Fly.Postgres("Db", { region: "iad", migrations: schema,});