Postgres
Source:
src/Railway/Postgres.ts
A Railway.Postgres is a Postgres-as-a-Service: the official
ghcr.io/railwayapp-templates/postgres-ssl:16 image, a Volume at
/var/lib/postgresql/data, POSTGRES_* / DATABASE_URL variables,
and an optional TCP proxy for the public URL.
Private hostname is {name}.railway.internal. From a
Service, yield ConnectPostgres. From a laptop, use
publicConnectionUri.
Create Postgres
Section titled “Create Postgres”Pass a Project. Alchemy generates a unique name, password, volume, and a public TCP proxy.
const site = yield* Railway.Project("Site");const db = yield* Railway.Postgres("Db", { project: site });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. The binding packs the private
URI ({name}.railway.internal).
import * as Drizzle from "alchemy/Drizzle/Postgres";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export default class Api extends Railway.Service<Api>()( "Api", { project: Site, main: import.meta.url, build: { install: ["pg"] } }, Effect.gen(function* () { const conn = yield* Railway.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(Railway.ConnectPostgresHttp)),) {}Public TCP
Section titled “Public TCP”public (default true) creates a TCP proxy on 5432.
publicConnectionUri is {domain}:{proxyPort} for laptop access
and deploy-time migrations.
const db = yield* Railway.Postgres("Db", { project: site, public: false,});Default is Postgres 16 with SSL. Pass image to pin another tag.
const db = yield* Railway.Postgres("Db", { project: site, image: "ghcr.io/railwayapp-templates/postgres-ssl:17",});Module-scope declarations
Section titled “Module-scope declarations”Resource-valued props accept the resource or an Effect producing it.
import * as Railway from "alchemy/Railway";
export const Site = Railway.Project("Site");export const Db = Railway.Postgres("Db", { project: Site });