## Database environments

Projects configure development by default. To add staging or production, run setup and choose **Add another environment**:

```sh npm
npx dinvolt setup
```

```sh bun
bunx dinvolt setup
```

```sh pnpm
pnpm dlx dinvolt setup
```

```sh yarn
yarn dlx dinvolt setup
```

Setup preserves customized code and existing migrations. Use a separate database for each environment.

### Credential names

Append the environment suffix to the **complete** variable name:

| Environment | Suffix        | Turso token example           |
| ----------- | ------------- | ----------------------------- |
| Development | None          | `TURSO_AUTH_TOKEN`            |
| Staging     | `_STAGING`    | `TURSO_AUTH_TOKEN_STAGING`    |
| Production  | `_PRODUCTION` | `TURSO_AUTH_TOKEN_PRODUCTION` |

For PlanetScale, production migrations use `DATABASE_URL_DIRECT_PRODUCTION`. There is no fallback to another environment's credentials or from the direct URL to the pooled URL.

### Choose the matching command

Commands without a suffix always target development, including when inherited environment variables select another destination.

Staging and production receive matching initialization, migration, Studio, and authentication scripts. For example, initialize an empty staging database with:

```sh npm
npm run db:init:staging
```

```sh bun
bun run db:init:staging
```

```sh pnpm
pnpm run db:init:staging
```

```sh yarn
yarn run db:init:staging
```

If development was not selected, its database operations fail. Static pages still work locally. Only the selected environments receive staging/production scripts.

### Runtime selection

| Where the app runs                              | Database environment |
| ----------------------------------------------- | -------------------- |
| Local development script                        | Development          |
| Vercel Preview                                  | Staging              |
| Vercel Production                               | Production           |
| Local build/start, without an explicit override | Development          |

Trusted server processes can explicitly set `DINVOLT_DATABASE_ENV`. `NODE_ENV` does not choose a database. Unknown or unconfigured environments fail on database use.

The runtime list lives in `src/db/environment.ts`; tool metadata lives in `.dinvolt/config.json`. Use setup to keep these, environment placeholders, and scripts aligned.

## Deploy on Vercel

After publishing your Git repository and its main branch, run:

```sh npm
npx dinvolt vercel setup
```

```sh bun
bunx dinvolt vercel setup
```

```sh pnpm
pnpm dlx dinvolt vercel setup
```

```sh yarn
yarn dlx dinvolt vercel setup
```

Configure database credentials in the matching Vercel scope:

| Vercel scope | Database variables           |
| ------------ | ---------------------------- |
| Preview      | Only `_STAGING` variables    |
| Production   | Only `_PRODUCTION` variables |

Leave `DINVOLT_DATABASE_ENV` unset for standard Preview/Production deployments. Enable Vercel's system environment variables; `VERCEL_ENV` selects the database.

All previews use staging by default. Use isolated preview databases if branches need separate data. Custom Vercel environments require an explicit staging or production mapping through `DINVOLT_DATABASE_ENV`.

Auth and email variables have **no database suffix**. Set them separately in each hosting environment:

```dotenv
BETTER_AUTH_URL=https://your-app.example.com
BETTER_AUTH_SECRET=replace-with-a-random-secret-of-at-least-32-characters
RESEND_API_KEY=your-resend-key
EMAIL_FROM="My App <accounts@example.com>"
NEXT_PUBLIC_SITE_URL=https://your-app.example.com
```

Preview derives its auth/site origin from the exact Vercel deployment URL. Local commands against staging/production also need that environment's auth configuration: changing the database target does not switch auth secrets or email settings.

Redeploy after changing Vercel variables. See the [authentication guide](/guides/authentication) for auth origins and email setup.

## Environment file loading

Database tools use Next.js's environment loader. Existing process variables take precedence over files.

| Target | File order, highest priority first |
| --- | --- |
| Development | `.env.development.local`, `.env.local`, `.env.development`, `.env` |
| Staging/production | `.env.production.local`, `.env.local`, `.env.production`, `.env` |

There is no automatic `.env.staging` loading. Test mode follows Next.js test-file rules and skips `.env.local`.

Keep the separately named credentials in your ignored `.env.local`, or inject the exact suffixed names in a trusted migration job. Check inherited variables before remote operations, and restart processes after credential changes.

Explicit script targets remain authoritative after files load. Keep environment selectors in trusted server configuration and secrets out of `NEXT_PUBLIC_*` variables.


