Skip to content

ConnectPostgres

Source: src/Railway/ConnectPostgres.ts

Bind a Postgres database to a Railway Service or Function and obtain the Effect-native connection string for Drizzle.Postgres / SQL.Postgres.

ConnectPostgres is the Context tag, the type, and the callable — yield* Railway.ConnectPostgres(Db). Provide ConnectPostgresHttp.

import * as Drizzle from "alchemy/Drizzle/Postgres";
export default class Query extends Railway.Function<Query>()(
"Query",
{ 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: db.execute("select 1 as ok").pipe(
Effect.flatMap(HttpServerResponse.json),
),
};
}).pipe(Effect.provide(Railway.ConnectPostgresHttp)),
) {}

ConnectPostgres packs a typed private URI onto the host. To store Railway’s template instead of a resolved URI (IaC db.env.DATABASE_URL), pass Railway.ref(Db, "DATABASE_URL") as a Variable value or Service.env entry. Railway interpolates ${{Db.DATABASE_URL}} at build/runtime.

const db = yield* Railway.Postgres("Db", { project: site });
yield* Railway.Variable("DatabaseUrl", {
project: site,
service: api,
name: "DATABASE_URL",
value: Railway.ref(db, "DATABASE_URL"),
});