# Use Neon Postgres with ViteHub

Use Neon through ViteHub's Postgres driver, which uses postgres.js and
Drizzle, not the Neon HTTP or WebSocket driver.

Start with the app from [Installation](https://vitehub.dev/docs/getting-started/installation).
Create a Neon branch for this check. Copy its connection string from the Neon
dashboard, with its TLS parameters.

## Install the Postgres dependencies

```bash [Terminal]
pnpm add postgres drizzle-orm
pnpm add -D drizzle-kit
```

Set `database: true` and `preset: 'node'` in your ViteHub options.

## Define a Postgres table

Create the Database Definition `server/databases/config.ts`. If it exists,
change it instead of adding a second one.

```ts [server/databases/config.ts]
import { defineDatabase } from 'vite-hub/database'
import { pgTable, serial, text } from 'drizzle-orm/pg-core'

export default defineDatabase({
	dialect: 'postgresql',
	connection: { url: process.env.DATABASE_URL },
	schema: {
		notes: pgTable('notes', {
			id: serial('id').primaryKey(),
			title: text('title').notNull(),
		}),
	},
})
```

Use the schema builders from `pg-core`. Changing the URL does not turn a SQLite
schema into a Postgres one.

Set `DATABASE_URL` in the migration shell and the server environment. Keep it
out of Git and client code.

## Apply migrations and query the database

Generate a new Postgres migration history. Do not apply SQLite migrations to
Postgres.

```bash [Terminal]
pnpm vitehub database generate
pnpm vitehub database migrate --dry-run
pnpm vitehub database migrate
```

The notes POST and GET routes from
[Save your first Database row](https://vitehub.dev/docs/database/get-started) work with this
table. Start the app, insert a note, read it back, and find the row in your
Neon branch.

## Choose the host connection

On Node, ViteHub uses one Postgres pool per process. On Vercel, set
`connection.url` in the server environment. Pick the endpoint with
[Neon's connection guide](https://neon.com/docs/connect/connect-from-any-app).

For Cloudflare Workers, check the
[Postgres and Hyperdrive contract](https://github.com/vite-hub/vitehub/tree/main/packages/database#postgres).
A Hyperdrive binding wins over the direct URL. Workers use one client per
request and do not share sockets.

ViteHub does not create Neon branches or offer Neon management APIs. See
[Database configuration](https://vitehub.dev/docs/database/configure) for migrations.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/guides.md)
- [Agents](https://vitehub.dev/raw/docs/guides/deploy-agent-cloudflare-vercel.md)
- [Chat bots](https://vitehub.dev/raw/docs/guides/slack-ai-bot.md)
- [Storage](https://vitehub.dev/raw/docs/guides/upstash-redis.md)
- Databases
  - [Turso](https://vitehub.dev/raw/docs/guides/turso.md)
  - [Neon Postgres](https://vitehub.dev/raw/docs/guides/neon-postgres.md) (this page)
  - [Cloudflare D1](https://vitehub.dev/raw/docs/guides/cloudflare-d1.md)
- [Server work](https://vitehub.dev/raw/docs/guides/resend.md)
- [Better Auth on Cloudflare](https://vitehub.dev/raw/docs/guides/better-auth-cloudflare.md)
- [Concepts](https://vitehub.dev/raw/docs/guides/rate-limiting-algorithms.md)

Next: [Cloudflare D1](https://vitehub.dev/raw/docs/guides/cloudflare-d1.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
