|
| 1 | +--- |
| 2 | +title: Alchemy |
| 3 | +description: Provision and manage Prisma Postgres projects, databases, and connections with Alchemy. |
| 4 | +metaTitle: Manage Prisma Postgres with Alchemy |
| 5 | +metaDescription: Provision and manage Prisma Postgres projects, databases, and connections with Alchemy. |
| 6 | +url: /postgres/iac/alchemy |
| 7 | +tocDepth: 3 |
| 8 | +toc: true |
| 9 | +--- |
| 10 | + |
| 11 | +Use [Alchemy](https://alchemy.run) to manage Prisma Postgres resources directly in your infrastructure code. |
| 12 | + |
| 13 | +Alchemy provides Prisma Postgres resources for: |
| 14 | + |
| 15 | +- Projects |
| 16 | +- Databases |
| 17 | +- Connections |
| 18 | +- Workspace references |
| 19 | + |
| 20 | +## Conceptual model |
| 21 | + |
| 22 | +[Alchemy](https://alchemy.run/) is a TypeScript library that creates and manages infrastructure when you run it. |
| 23 | + |
| 24 | +Instead of a separate declarative config format, you write a normal TypeScript program (commonly `alchemy.run.ts`) and execute it. |
| 25 | + |
| 26 | +Alchemy resources follow lifecycle phases (`create`, `update`, `delete`) and manage provider APIs for you: |
| 27 | + |
| 28 | +- You compose resources in code (`Project`, `Database`, `Connection`). |
| 29 | +- Alchemy handles dependency ordering between those resources. |
| 30 | +- Resource defaults can include safety behavior, such as delete protection on projects and databases. |
| 31 | +- `await app.finalize()` cleans up orphaned resources that are no longer in your program. |
| 32 | + |
| 33 | +This makes it useful when you want infrastructure code that feels close to your application runtime and platform integrations. |
| 34 | + |
| 35 | +## When to use Alchemy |
| 36 | + |
| 37 | +Alchemy is a strong fit when: |
| 38 | + |
| 39 | +- You are already deploying with Alchemy and want Prisma Postgres in the same graph. |
| 40 | +- You want resource composition with first-class platform integrations (for example, Hyperdrive + Workers). |
| 41 | +- You prefer lifecycle-driven resource code with safe deletion defaults. |
| 42 | + |
| 43 | +## Typical workflow |
| 44 | + |
| 45 | +If you follow the Alchemy getting started flow, the common lifecycle is: |
| 46 | + |
| 47 | +Create a project scaffold (optional): |
| 48 | + |
| 49 | +```npm |
| 50 | +npx alchemy@latest create --template typescript |
| 51 | +``` |
| 52 | + |
| 53 | +Configure provider profiles and credentials: |
| 54 | + |
| 55 | +```npm |
| 56 | +npm run alchemy configure |
| 57 | +``` |
| 58 | + |
| 59 | +Authenticate (required for Cloudflare resources): |
| 60 | + |
| 61 | +```npm |
| 62 | +npm run alchemy login |
| 63 | +``` |
| 64 | + |
| 65 | +Start local development with hot reload: |
| 66 | + |
| 67 | +```npm |
| 68 | +npm run alchemy dev |
| 69 | +``` |
| 70 | + |
| 71 | +Deploy: |
| 72 | + |
| 73 | +```npm |
| 74 | +npm run alchemy deploy |
| 75 | +``` |
| 76 | + |
| 77 | +Tear down: |
| 78 | + |
| 79 | +```npm |
| 80 | +npm run alchemy destroy |
| 81 | +``` |
| 82 | + |
| 83 | +For Prisma Postgres-only resources, `configure`/`login` may not be necessary in every setup. They are typically needed when you also manage Cloudflare resources in the same app. |
| 84 | + |
| 85 | +## Prerequisites |
| 86 | + |
| 87 | +- An [Alchemy project](https://alchemy.run/) |
| 88 | +- A Prisma service token |
| 89 | +- `PRISMA_SERVICE_TOKEN` configured in your environment |
| 90 | +- `ALCHEMY_PASSWORD` configured when your resources contain secrets |
| 91 | + |
| 92 | +## Authentication |
| 93 | + |
| 94 | +Alchemy reads Prisma credentials from environment variables by default. |
| 95 | + |
| 96 | +```bash |
| 97 | +export PRISMA_SERVICE_TOKEN="prsc_your_token_here" |
| 98 | +export ALCHEMY_PASSWORD="choose-a-strong-password" |
| 99 | +``` |
| 100 | + |
| 101 | +If you need multiple workspaces/accounts, you can override auth per resource with `serviceToken`. |
| 102 | + |
| 103 | +`ALCHEMY_PASSWORD` is used to encrypt/decrypt secret values in Alchemy state. |
| 104 | + |
| 105 | +## Minimal example |
| 106 | + |
| 107 | +```ts file=alchemy.run.ts |
| 108 | +import alchemy from "alchemy"; |
| 109 | +import { Connection, Database, Project } from "alchemy/prisma-postgres"; |
| 110 | + |
| 111 | +const app = await alchemy("prisma-postgres-example"); |
| 112 | + |
| 113 | +const project = await Project("project"); |
| 114 | + |
| 115 | +const database = await Database("database", { |
| 116 | + project, |
| 117 | + region: "us-east-1", |
| 118 | +}); |
| 119 | + |
| 120 | +const connection = await Connection("connection", { |
| 121 | + database, |
| 122 | + name: "app-connection", |
| 123 | +}); |
| 124 | + |
| 125 | +export const projectId = project.id; |
| 126 | +export const databaseId = database.id; |
| 127 | +export const host = connection.host; |
| 128 | +export const user = connection.user; |
| 129 | +export const connectionString = connection.connectionString; |
| 130 | +export const prismaConnectionString = connection.prismaConnectionString; |
| 131 | + |
| 132 | +await app.finalize(); |
| 133 | +``` |
| 134 | + |
| 135 | +## Complete example with Hyperdrive + Worker |
| 136 | + |
| 137 | +```ts file=alchemy.run.ts |
| 138 | +import alchemy from "alchemy"; |
| 139 | +import { Hyperdrive, Worker } from "alchemy/cloudflare"; |
| 140 | +import { Connection, Database, Project } from "alchemy/prisma-postgres"; |
| 141 | + |
| 142 | +const app = await alchemy("prisma-postgres-example"); |
| 143 | + |
| 144 | +const project = await Project("project"); |
| 145 | + |
| 146 | +const database = await Database("database", { |
| 147 | + project, |
| 148 | + region: "us-east-1", |
| 149 | +}); |
| 150 | + |
| 151 | +const connection = await Connection("connection", { |
| 152 | + database, |
| 153 | + name: "connection", |
| 154 | +}); |
| 155 | + |
| 156 | +const db = await Hyperdrive("prisma-postgres", { |
| 157 | + origin: connection.connectionString.unencrypted, |
| 158 | +}); |
| 159 | + |
| 160 | +export const worker = await Worker("worker", { |
| 161 | + entrypoint: "src/worker.ts", |
| 162 | + bindings: { |
| 163 | + HYPERDRIVE: db, |
| 164 | + }, |
| 165 | + compatibilityFlags: ["nodejs_compat"], |
| 166 | +}); |
| 167 | + |
| 168 | +await app.finalize(); |
| 169 | +``` |
| 170 | + |
| 171 | +```ts file=src/worker.ts |
| 172 | +import { Client } from "pg"; |
| 173 | +import type { worker } from "../alchemy.run.ts"; |
| 174 | + |
| 175 | +export default { |
| 176 | + async fetch(_request: Request, env: typeof worker.Env): Promise<Response> { |
| 177 | + const client = new Client({ |
| 178 | + connectionString: env.HYPERDRIVE.connectionString, |
| 179 | + }); |
| 180 | + |
| 181 | + try { |
| 182 | + await client.connect(); |
| 183 | + const result = await client.query("SELECT * FROM pg_tables"); |
| 184 | + |
| 185 | + return Response.json({ |
| 186 | + success: true, |
| 187 | + result: result.rows, |
| 188 | + }); |
| 189 | + } catch (error: any) { |
| 190 | + return new Response(`Database error: ${error.message}`, { status: 500 }); |
| 191 | + } |
| 192 | + }, |
| 193 | +}; |
| 194 | +``` |
| 195 | + |
| 196 | +## Working with multiple workspaces |
| 197 | + |
| 198 | +Prisma service tokens are workspace-scoped. You can pass different tokens to different resources: |
| 199 | + |
| 200 | +```ts |
| 201 | +import alchemy from "alchemy"; |
| 202 | +import { Project } from "alchemy/prisma-postgres"; |
| 203 | + |
| 204 | +const app = await alchemy("prisma-workspaces"); |
| 205 | + |
| 206 | +const workspaceAProject = await Project("workspace-a-project", { |
| 207 | + serviceToken: alchemy.env.PRISMA_SERVICE_TOKEN_WORKSPACE_A, |
| 208 | +}); |
| 209 | + |
| 210 | +const workspaceBProject = await Project("workspace-b-project", { |
| 211 | + serviceToken: alchemy.env.PRISMA_SERVICE_TOKEN_WORKSPACE_B, |
| 212 | +}); |
| 213 | + |
| 214 | +await app.finalize(); |
| 215 | +``` |
| 216 | + |
| 217 | +If you need to resolve a workspace by name or id, use `WorkspaceRef`: |
| 218 | + |
| 219 | +```ts |
| 220 | +import { WorkspaceRef } from "alchemy/prisma-postgres"; |
| 221 | + |
| 222 | +const workspace = await WorkspaceRef("my-workspace"); |
| 223 | +``` |
| 224 | + |
| 225 | +## Deletion behavior |
| 226 | + |
| 227 | +`Project` and `Database` default to delete protection in Alchemy. |
| 228 | + |
| 229 | +- `Project`: `delete` defaults to `false` |
| 230 | +- `Database`: `delete` defaults to `false` |
| 231 | + |
| 232 | +For ephemeral environments, set `delete: true` explicitly: |
| 233 | + |
| 234 | +```ts |
| 235 | +const testDatabase = await Database("test-db", { |
| 236 | + project, |
| 237 | + region: "us-east-1", |
| 238 | + delete: true, |
| 239 | +}); |
| 240 | +``` |
| 241 | + |
| 242 | +## Common troubleshooting |
| 243 | + |
| 244 | +### Missing token |
| 245 | + |
| 246 | +If resource creation fails with an auth error, confirm `PRISMA_SERVICE_TOKEN` is set for the process running Alchemy. |
| 247 | + |
| 248 | +### Wrong workspace |
| 249 | + |
| 250 | +Prisma service tokens are workspace-scoped. If resources appear in the wrong workspace, use per-resource `serviceToken` overrides. |
| 251 | + |
| 252 | +## References |
| 253 | + |
| 254 | +- [Alchemy](https://alchemy.run) |
| 255 | +- [Alchemy getting started](https://alchemy.run/getting-started) |
| 256 | +- [What is Alchemy?](https://alchemy.run/what-is-alchemy/) |
| 257 | +- [Alchemy package on npm](https://www.npmjs.com/package/alchemy) |
| 258 | +- [Prisma Postgres](https://www.prisma.io/postgres) |
0 commit comments