Postgres
Source:
src/Fly/Postgres.ts
A Fly.Postgres is a Managed Postgres (MPG) cluster. It is billed.
Do not wrap unmanaged fly postgres.
Create a cluster
Section titled “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",});plan is the hardware size: basic, starter, launch,
scale, performance. Default is basic.
const db = yield* Fly.Postgres("Db", { region: "iad", plan: "starter",});Region
Section titled “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",});Volume size
Section titled “Volume size”volumeSizeGb is the initial disk. Fly defaults to 10 GB.
const db = yield* Fly.Postgres("Db", { region: "iad", volumeSizeGb: 20,});PostGIS
Section titled “PostGIS”postgis: true enables PostGIS at create.
const db = yield* Fly.Postgres("Db", { region: "iad", postgis: true,});Connect from a Service
Section titled “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)),) {}Migrations
Section titled “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,});