@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
mdx 144 lines 4.17 KB
Raw
1 ---
2 title: Postgres
3 description: Official SSL Postgres in a Project. Bind Railway.ConnectPostgres and query with Drizzle or SQL.
4 ---
5
6 A [`Railway.Postgres`](/providers/railway/postgres) is Postgres as a
7 Service: the official
8 `ghcr.io/railwayapp-templates/postgres-ssl:16` image, a Volume at
9 `/var/lib/postgresql/data`, `POSTGRES_*` / `DATABASE_URL` variables,
10 and an optional TCP proxy for the public URL.
11
12 Private hostname is `{name}.railway.internal`. From a
13 [Service](/railway/compute/services), yield
14 [`ConnectPostgres`](/providers/railway/connectpostgres). From a
15 laptop, use `publicConnectionUri`.
16
17 ## Create Postgres
18
19 Pass a Project. Alchemy generates a unique name, password, volume,
20 and a public TCP proxy.
21
22 ```typescript
23 const site = yield* Railway.Project("Site");
24 const db = yield* Railway.Postgres("Db", { project: site });
25 ```
26
27 :::caution[Changing `project` replaces Postgres]
28 A new service + volume are created in the new Project. The old
29 service and volume are deleted. Data is not copied.
30 :::
31
32 ## Connect from a Service
33
34 Yield `ConnectPostgres` in the Service's constructor. Provide
35 `ConnectPostgresHttp`. Pass `conn.connectionString` to Drizzle or
36 SQL.
37
38 ```typescript
39 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
40 import * as HttpServerResponse from "effect/http/HttpServerResponse";
41
42 export default class Api extends Railway.Service<Api>()(
43 "Api",
44 {
45 project: Site,
46 main: import.meta.url,
47 },
48 Effect.gen(function* () {
49 const conn = yield* Railway.ConnectPostgres(Db);
50 const db = yield* Drizzle.Postgres(conn.connectionString);
51 return {
52 fetch: Effect.gen(function* () {
53 const rows = yield* db.execute("select 1 as ok");
54 return HttpServerResponse.json({ rows });
55 }),
56 };
57 }).pipe(Effect.provide(Railway.ConnectPostgresHttp)),
58 ) {}
59 ```
60
61 `connectionString` is the private URI
62 (`{name}.railway.internal:5432`). Both come from `Railway.Postgres`,
63 packed into the Service env — not `Config.Redacted`.
64
65 To store Railway's `${{Db.DATABASE_URL}}` template instead of a
66 resolved URI, pass [`Railway.ref`](/providers/railway/ref)
67 (`Railway.ref(Db, "DATABASE_URL")`) as a
68 [Variable](/railway/data/variables) `value`.
69
70 ## Public TCP
71
72 `public` (default `true`) creates a TCP proxy on 5432.
73 `publicConnectionUri` is `{domain}:{proxyPort}` for laptop access
74 and deploy-time migrations. In-service connections always use the
75 private hostname.
76
77 ```typescript
78 const db = yield* Railway.Postgres("Db", {
79 project: site,
80 public: false,
81 });
82 ```
83
84 Attach a standalone [`TcpProxy`](/railway/networking#tcp-proxies)
85 when you need public TCP on a Redis service, or on Postgres created
86 with `public: false`.
87
88 ## Image
89
90 Default is Postgres 16 with SSL. Pass `image` to pin another tag.
91
92 ```typescript
93 const db = yield* Railway.Postgres("Db", {
94 project: site,
95 image: "ghcr.io/railwayapp-templates/postgres-ssl:17",
96 });
97 ```
98
99 ## Superuser
100
101 `user` (default `postgres`) and `database` (default `railway`) are
102 create-only. Wrap `password` with `Redacted.make(...)`. If omitted,
103 a password is generated on first create.
104
105 ```typescript
106 import * as Redacted from "effect/Redacted";
107
108 const db = yield* Railway.Postgres("Db", {
109 project: site,
110 user: "app",
111 database: "app",
112 password: Redacted.make("…"),
113 });
114 ```
115
116 ## Module-scope declarations
117
118 Resource-valued props accept the resource or an Effect producing it.
119
120 ```typescript
121 // src/db.ts
122 import * as Railway from "@alchemy.run/railway";
123
124 export const Site = Railway.Project("Site");
125 export const Db = Railway.Postgres("Db", { project: Site });
126 ```
127
128 ## Where next
129
130 [Regions](/railway/compute/regions) lists codes.
131 [Services](/railway/compute/services) is the container that binds
132 the cluster.
133 [MySQL](/railway/data/mysql) and [Mongo](/railway/data/mongo) are the
134 same shape for those engines.
135 [Drizzle on Postgres](/sql/drizzle/postgres) is the schema-to-query
136 flow.
137 [Variables](/railway/data/variables) covers `Config.Redacted` and
138 `Railway.ref`.
139 The [`Postgres` reference](/providers/railway/postgres) lists every
140 prop.
141 The [`ConnectPostgres` reference](/providers/railway/connectpostgres)
142 is the runtime binding.
143 [Example: railway-service](https://github.com/alchemy-run/alchemy/tree/main/examples/railway-service)
144 is the complete runnable project.