# Add a Seed Script to a Drizzle ORM Project

Yes. An agent writes a seed script for your Drizzle schema, makes it rerunnable, proves it against a fresh Postgres and a drizzle-kit push, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-seed-data/drizzle/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the database package is wired before writing a row. In a monorepo like t3-oss/create-t3-turbo the package is `packages/db`: `src/client.ts` creates the client with `drizzle({ client: sql, schema, casing: 'snake_case' })` over `@vercel/postgres`, `drizzle.config.ts` reads `POSTGRES_URL` and rewrites the pooler port `6543` to `5432` for a direct connection, and the scripts are `push` (`pnpm with-env drizzle-kit push`) and `studio`, where `with-env` is `dotenv -e ../../.env --`. There are no migration files; the schema is applied with `push`. `src/schema.ts` defines `Post` with a random `uuid` id, a `varchar(256)` title, `text` content and timestamps, and `src/auth-schema.ts` holds the `user`, `session` and `account` tables that better-auth owns. `.env.example` points `POSTGRES_URL` at a hosted pooler, and `.github/workflows/ci.yml` copies that file to `.env` before lint, format and typecheck.

From that it writes `packages/db/src/seed.ts`. The script builds the same client as `client.ts` so the `snake_case` casing matches, then inserts a handful of users through the `user` table with `onConflictDoNothing` on the unique `email`, and a set of posts whose `createdAt` values are spread over the past weeks so date ordering is visible in the apps. Ids are fixed strings rather than random so the script is idempotent: running it twice leaves the same rows. Nothing touches `session` or `account`, because sign-in state belongs to better-auth at runtime and a seeded session would be a credential in the repository.

The script gets a `seed` entry in `packages/db/package.json` as `pnpm with-env tsx src/seed.ts`, with `tsx` added as a devDependency, and the root gains a `db:seed` script that runs it through turbo with `cache: false` in `turbo.json`, beside the existing `push` and `studio` tasks. A guard at the top refuses to run when `POSTGRES_URL` points at a host that is not local unless `SEED_CONFIRM=1` is set, since the same variable also names production.

Then it proves the whole path in the sandbox: a Postgres installed with `apt-get`, since the sandbox has no Docker, `POSTGRES_URL` pointed at it, `pnpm db:push` to create the tables, `pnpm db:seed` twice, and a `select count(*)` on `post` and `user` after each run showing the counts unchanged by the second. `pnpm typecheck` and `pnpm lint` run last, since the seed file is part of the package the workspace checks.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a seed script to packages/db for this Drizzle schema. Build the client the same way src/client.ts does, insert a few users into the user table with onConflictDoNothing on email and a set of posts with fixed ids and createdAt values spread over recent weeks, so the script is idempotent. Do not seed session or account rows. Add a seed script to the package that runs it with tsx through with-env, a root db:seed task in turbo.json with cache off, and a guard that refuses a non-local POSTGRES_URL unless SEED_CONFIRM=1. Prove it against a local Postgres: run drizzle-kit push, run the seed twice, and show the row counts after each run. Run pnpm typecheck and pnpm lint. Open a pull request quoting the counts and the check results.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `packages/db/src/seed.ts`, the `seed` script and `tsx` devDependency in `packages/db/package.json`, a `db:seed` script in the root `package.json` and its task in `turbo.json`, and a line in the README under the database setup. Its description quotes the `drizzle-kit push` output, the two seed runs with the `post` and `user` counts after each, and the typecheck and lint results.

A reviewer reads `seed.ts` once for what it inserts and confirms it leaves `session` and `account` alone, then checks the guard so a teammate with a production URL in `.env` cannot seed it by accident. The second-run counts are the proof of idempotency, and they are in the description rather than claimed. The transcript at GET /o/{org}/sessions/{id}/messages has the container start, the push and both seed runs in full.

Nothing is written to any database you own. The sandbox's Postgres is gone with the run; seeding your own development database is `pnpm db:seed` after the merge.

## Run it on every new preview database

A preview environment with an empty database is where a seed script earns its place. When your platform creates one, send POST /o/{org}/sessions to the Project whose secrets hold that database's `POSTGRES_URL`, with a prompt that runs `pnpm db:push` and `pnpm db:seed` with `SEED_CONFIRM=1` and reports the counts; the guard is why the confirmation is explicit. A `request_id` that is a UUID v5 of the preview's name keeps a retried send from running twice.

A webhook URL on the API key delivers `run.completed` with the session URL once the seed has run, so the preview's status can flip to ready from the handler rather than from a poll.

## Why run it on Rush?

The script is short; the confidence comes from running it against a database that started empty and running it again, which on a laptop means a container, a `.env` swap and the risk of pointing `POSTGRES_URL` at the wrong host. The sandbox has its own Postgres for the duration of the run, the counts after each pass are in the transcript, and your own `.env` is never involved.

A seed script with a guard, a fixed set of ids and a quoted second-run result is one a team can rely on in CI and in preview environments. Several packages in a workspace, or several repositories on the same schema conventions, are separate runs with separate proofs.

## Frequently asked questions

### Will it use drizzle-seed instead of hand-written rows?

Only if you ask. Fixed rows with known ids make the apps' screens predictable and the script idempotent by construction; `drizzle-seed` generates volume from a seed number, which suits load testing better. Name it in the prompt and the agent writes the `seed(db, schema).refine(...)` form instead.

### Why does it skip the auth tables?

`session` and `account` hold tokens and provider links that better-auth creates at sign-in. Seeding them would mean committing credentials and bypassing the auth flow. Seeding `user` rows is enough for the apps to show authored content.

### Can it run in CI?

Yes. The CI workflow already copies `.env.example` to `.env`; add a Postgres service to a job, set `POSTGRES_URL` to it, and run `pnpm db:push` then `pnpm db:seed`. The guard passes because the host is local. Ask in a follow-up and the agent adds that job.

### What stops someone from seeding production?

The guard at the top of `seed.ts` reads `POSTGRES_URL`, and unless the host is local it exits with a message unless `SEED_CONFIRM=1` is set. The script never contains a connection string of its own.

## More Drizzle ORM jobs

- [Add Slow Query Logging to a Drizzle App](https://getrush.ai/run/add-slow-query-logging/drizzle/)
- [Add Soft Delete to a Drizzle Schema](https://getrush.ai/run/add-soft-delete/drizzle/)
- [Wrap Multi-Step Writes in Transactions with Drizzle](https://getrush.ai/run/add-transactions/drizzle/)

## The same job on other stacks

- [Add Seed Data to a GORM App](https://getrush.ai/run/add-seed-data/gorm/)
- [Add Seed Data to a Mongoose App](https://getrush.ai/run/add-seed-data/mongoose/)
- [Add a Seed Script to a Prisma Project](https://getrush.ai/run/add-seed-data/prisma/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
