TypeScript
---
title: Drizzle Postgres on Fly
description: Connect a Fly Service to Managed Postgres and deploy reviewed Drizzle migrations.
---
Run Drizzle in a `Fly.Service`, with [Fly Managed Postgres](/fly/data/postgres) on the same organization's private network. Start with [Fly setup](/fly/setup), the [Service tutorial](/fly/tutorial/part-1), and a `src/schema.ts` exporting `Users` from the [Postgres client guide](/sql/drizzle/postgres).
## Install the database dependencies
```sh
pnpm add @alchemy.run/drizzle [email protected] @effect/sql-pg
pnpm add -D [email protected]
```
The Drizzle versions match the repository's Effect integration.
## Configure migration generation
```typescript
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
out: "./drizzle",
dbCredentials: { url: process.env.DATABASE_URL! },
});
```
`DATABASE_URL` is the direct Postgres URL used by the migration runner, not the pooled runtime URL.
## Generate after changing the schema
```sh
pnpm exec drizzle-kit generate
```
Review the generated SQL and snapshots, including any rename decisions, before applying them.
## Commit the migration
```sh
git add src/schema.ts drizzle.config.ts drizzle
git diff --cached
```
```sh
git commit -m "Add users migration"
```
Commit the schema, SQL, and snapshots together; CI deploys this reviewed history rather than generating it.
## Declare the cluster
```typescript
// src/db.ts
import * as Fly from "@alchemy.run/fly";
export const Db = Fly.Postgres("Db", { region: "iad" });
```
This provisions a billed Managed Postgres cluster, not an unmanaged Postgres Machine.
## Bind the cluster
```diff lang="typescript"
+import { Db } from "./db.ts";
Effect.gen(function* () {
+ const conn = yield* Fly.ConnectPostgres(Db);
return {
fetch: Effect.succeed(HttpServerResponse.text("Hello from Fly!")),
};
-}),
+}).pipe(Effect.provide(Fly.ConnectPostgresHttp)),
```
The binding attaches the cluster to the Service's App and supplies pooled and direct connection Effects. These excerpts use the tutorial's minimal text handler until the database is migrated.
## Provision before the first migration
```sh
pnpm alchemy deploy
```
Keep the tutorial's `yield* Api` in the Stack, using `Fly.providers()`. This first deployment creates the database and a handler that does not yet query application tables.
## Apply the committed SQL
```sh
pnpm exec drizzle-kit migrate
```
Supply the cluster's `connectionUri` as the migration runner's secret `DATABASE_URL`, and run where the direct hostname resolves and is reachable on the Fly organization network, such as a correctly configured WireGuard peer or a Machine on 6PN. A pooled URI is not the migration endpoint.
## Open the runtime client
```diff lang="typescript"
+import * as Drizzle from "@alchemy.run/drizzle/Postgres";
const conn = yield* Fly.ConnectPostgres(Db);
+const db = yield* Drizzle.Postgres(conn.connectionString);
```
The pooled connection opens lazily when a request first queries the database.
## Query from the Service
```diff lang="typescript"
+import { Users } from "./schema.ts";
return {
- fetch: Effect.succeed(HttpServerResponse.text("Hello from Fly!")),
+ fetch: Effect.gen(function* () {
+ const users = yield* db.select().from(Users).pipe(Effect.orDie);
+ return yield* HttpServerResponse.json({ users });
+ }),
};
```
Queries run inside the request scope, rather than during infrastructure construction.
## Deploy the application
```sh
pnpm alchemy deploy
```
For subsequent schema changes, generate, review, and commit first, then apply the committed migrations before deploying compatible application code. `alchemy deploy` does not replace `drizzle-kit migrate` in this guide.
## Choose one migration owner
`Fly.Postgres` also supports a `migrations` directory, but this guide deliberately leaves it unset and keeps Drizzle as the migration owner. Switching an existing database to Alchemy's migration history is a separate [migration adoption decision](/sql/drizzle/migrations#existing-migration-history), not a command substitution.
## Continue
See the [Managed Postgres guide](/fly/data/postgres), [portable Postgres queries](/sql/drizzle/postgres), and [migration workflow](/sql/drizzle/migrations). The [fly-postgres example](https://github.com/alchemy-run/alchemy/tree/main/examples/fly-postgres) demonstrates the Service binding, but its test-only HTTP migration endpoint is not used here.