> Full Neon documentation index: https://neon.com/docs/llms.txt

# Migrate from Vercel Postgres SDK to the Neon serverless driver

Move your application from the Vercel Postgres SDK to the Neon serverless driver

Vercel transitioned all Vercel Postgres stores to Neon's native integration in the [Vercel Marketplace](https://vercel.com/blog/introducing-the-vercel-marketplace) (see the [Vercel Postgres transition guide](https://neon.com/docs/guides/vercel-postgres-transition-guide)). This guide shows how to migrate your code from the Vercel Postgres SDK [(@vercel/postgres)](https://vercel.com/docs/storage/vercel-postgres/sdk) to the [Neon serverless driver](https://github.com/neondatabase/serverless).

## Why migrate?

The Neon serverless driver lets you choose between HTTP for one-shot queries and non-interactive transactions, or WebSockets for sessions, interactive transactions, and full [node-postgres](https://node-postgres.com/) compatibility. Neon actively maintains it.

## Prerequisites

To begin, you'll need:

- An existing application using the Vercel Postgres SDK
- A [Neon account](https://neon.com/docs/get-started/signing-up) (Vercel Postgres databases have already moved to Neon)

## Migration steps

### 1. Install the Neon serverless driver

Start by installing the Neon serverless driver in your project:

```bash
npm install @neondatabase/serverless
```

**Important:** The examples in this guide read the connection string from a `DATABASE_URL` environment variable. Make sure that variable is set in your environment, or change the examples to match the variable name you use.

### 2. Update your database connection

Replace your Vercel Postgres SDK imports and connection setup with the Neon serverless driver. You have two options:

#### Option A: Using HTTP (recommended for one-shot queries)

```diff
import { sql } from '@vercel/postgres';

import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
```

#### Option B: Using WebSockets (recommended for interactive transactions)

```diff
import { db } from '@vercel/postgres';

import ws from 'ws';
import { Pool, neonConfig } from '@neondatabase/serverless';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
neonConfig.webSocketConstructor = ws;
```

### 3. Update your queries

Here are common query patterns and how to migrate them:

#### Simple queries

```diff
# Vercel Postgres SDK
const { rows } = await sql`SELECT * FROM users WHERE id = ${userId}`;

# Neon HTTP
const rows = await sql`SELECT * FROM users WHERE id = ${userId}`;

# Neon WebSockets
const { rows } = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
```

#### Transactions

An interactive transaction must run all its statements on one connection, so check out a client with `pool.connect()` instead of calling `pool.query()` for each statement:

```diff
 import { db } from '@vercel/postgres';

async function transferFunds(fromId: number, toId: number, amount: number) {
  const client = await db.connect();
  try {
    await client.query('BEGIN');
    await client.query('UPDATE accounts SET balance = balance - $1 WHERE id = $2', [
      amount,
      fromId,
    ]);
    await client.query('UPDATE accounts SET balance = balance + $1 WHERE id = $2', [amount, toId]);
    await client.query('COMMIT');
  } catch (e) {
    await client.query('ROLLBACK');
    throw e;
  } finally {
    client.release();
  }
}

import { Pool } from '@neondatabase/serverless';

async function transferFunds(fromId: number, toId: number, amount: number) {
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });
  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    await client.query('UPDATE accounts SET balance = balance - $1 WHERE id = $2', [amount, fromId]);
    await client.query('UPDATE accounts SET balance = balance + $1 WHERE id = $2', [amount, toId]);
    await client.query('COMMIT');
  } catch (e) {
    await client.query('ROLLBACK');
    throw e;
  } finally {
    client.release();
    await pool.end();
  }
}
```

## Best practices

1. **Choose the right connection method**:
   - Use HTTP (`neon()`) for one-shot queries and non-interactive transactions with [`sql.transaction()`](https://neon.com/docs/serverless/serverless-driver#issue-multiple-queries-with-the-transaction-function).
   - Use WebSockets (`Pool`) for interactive transactions and session-based operations.

2. **Connection management**:

   - For HTTP queries, reuse the `sql` query function.
   - In serverless environments, WebSocket connections can't outlive a single request. Create, use, and close the `Pool` inside the request handler:

   ```typescript
   const pool = new Pool({ connectionString: process.env.DATABASE_URL });
   try {
     // Your queries here
   } finally {
     await pool.end();
   }
   ```

3. **Error handling**:
   ```typescript
   try {
     const result = await sql`SELECT * FROM users`;
     return result;
   } catch (error) {
     console.error('Database error:', error);
     throw new Error('Failed to fetch users');
   }
   ```

## Working with ORMs

Neon's serverless driver is compatible with popular ORMs like Prisma and Drizzle ORM. Check out the following guides to learn more:

- [Prisma](https://neon.com/docs/guides/prisma): Learn how to connect to Neon from Prisma
- [Drizzle ORM](https://orm.drizzle.team/docs/tutorials/drizzle-with-neon): Learn how to connect to Neon from Drizzle ORM

## Advanced configuration

Most applications don't need advanced configuration. For custom setups or troubleshooting, these are the key options:

- **poolQueryViaFetch**: Setting `poolQueryViaFetch` to true sends `Pool.query()` calls as low-latency `HTTP` fetch requests (currently defaults to false).

- **wsProxy**: This option is for connecting via a WebSocket proxy deployed in front of your own Postgres instance, which allows you to use the Neon serverless driver with a local development environment.

For more information about these options, see [Advanced configuration](https://github.com/neondatabase/serverless/blob/main/CONFIG.md#advanced-configuration).

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/guides/vercel-sdk-migration"}` to https://feedback.neon.tech/ (no auth required).
