# Preview deployments (/docs/cli/deploy/preview)

`pnpm clubedge deploy --preview` deploys the current Git branch next to production, to try a change before it ships. It works on Vercel and Railway, after a first `pnpm clubedge deploy`, and needs CLI 0.30.0 or newer (0.30.1 on Railway). It deploys a web app: on a [frontend and its API](../frontend-and-api.mdx), or a project [without a website](../without-a-website.mdx), it refuses, and prints how to copy production's Railway environment instead.

```sh
git switch -c new-checkout
pnpm clubedge deploy --preview
```

Production is not changed.

## A database branch per preview [#a-database-branch-per-preview]

With `NEON_API_KEY` and `NEON_PROJECT_ID`, in the shell or in `.env.production`, the preview gets its own [Neon branch](https://neon.com/docs/introduction/branching): a copy of the production database made in seconds, which shares its storage until it changes. Migrations that add, change, or drop tables run against that copy, never against production.

```sh title=".env.production"
NEON_API_KEY=napi_...                 # Neon console: Account settings, API keys
NEON_PROJECT_ID=late-dream-12345678   # the project's Settings, General
```

`deploy --preview`:

1. creates the branch `preview/<git branch>`, or reuses it on later runs so the preview keeps its data;
2. applies this branch's migrations to it, from your machine;
3. points the preview's `DATABASE_URL` (pooled) and `DATABASE_URL_DIRECT` at the branch.

The role and database come from production's Neon URL, or Neon's defaults (`neondb_owner`, `neondb`). A free database the CLI created must be [claimed](../neon.mdx) first, since only a Neon account has an API key.

## Per platform [#per-platform]

<Tabs items={['Vercel', 'Railway']}>
  <Tab value="Vercel">
    The preview is a Vercel preview deployment, with the project's Preview variables. The branch's URLs are set as Preview variables of your Git branch, through Vercel's API, so they stay off the command line and a redeploy from the dashboard keeps them. That needs the Vercel project connected to its Git repository: in Vercel, open the project's **Settings**, then **Git**. Without it, `deploy --preview` stops before deploying and says so.

    Without the Neon settings, the preview uses the Preview `DATABASE_URL`. The deployment gets `CLIENT_IP_HEADER=x-real-ip`, as production does (CLI 0.42.0 or newer), since previews don't get production's variables.
  </Tab>
  <Tab value="Railway">
    The preview is a Railway environment named `preview-<git branch>`, copied from production, with its own `*.up.railway.app` domain. `deploy` sets its public URL, its `PORT`, `CLIENT_IP_HEADER=x-real-ip` as for production (CLI 0.42.0 or newer), and with the Neon settings its database URLs, then deploys and waits for the release.

    Without the Neon settings, the environment gets its own empty copy of Railway's Postgres, migrated before the release. When production's database comes from outside Railway, `deploy --preview` refuses: the copy would use production's data.
  </Tab>
</Tabs>

## Clean up [#clean-up]

Previews stay until you remove them. Delete the branch `preview/<git branch>` in the Neon console and, on Railway, the environment:

```sh
railway environment delete preview-<git branch> --yes
```

For a preview of every pull request, made and removed by GitHub Actions, see [Preview environments](../../starter/deploy/preview-environments.mdx).
