With Vercel Postgres transitioning to Neon's native integration in the Vercel Marketplace, now is the perfect time to migrate from the Vercel Postgres SDK (@vercel/postgres) to the Neon serverless driver.
Why migrate?
Switching to the Neon serverless driver provides several advantages. It offers greater flexibility by allowing the choice between HTTP for single queries or WebSockets for transactions and full node-postgres compatibility. Additionally, it enhances maintainability by relying on Neon's actively maintained, native database driver.
Prerequisites
To begin, you’ll need:
- An existing application using the Vercel Postgres SDK
- A Neon account (your Vercel Postgres database will automatically migrate to Neon)
Migration Steps
1. Install the Neon serverless driver
Start by installing the Neon serverless driver in your project:
important
To ensure proper configuration, set your environment variable to DATABASE_URL
when referencing the database URL in your code, especially if you're following this guide.
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 simple queries)
Option B: Using WebSockets (Recommended for transactions)
3. Update your queries
Here are common query patterns and how to migrate them:
Simple Queries
Transactions
Best practices
-
Choose the right connection method:
- Use HTTP (
neon()
) for single queries and simple transactions. - Use WebSockets (
Pool
) for complex transactions and session-based operations.
- Use HTTP (
-
Connection management:
- For HTTP queries, reuse the
sql
query function. - For WebSocket connections in serverless environments, always close connections:
- For HTTP queries, reuse the
-
Error Handling:
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
Learn how to connect to Neon from Prisma
Drizzle ORM
Learn how to connect to Neon from Drizzle ORM
Advanced Configuration
For most cases, using neon serverless driver is straightforward without needing advanced configuration. However, for custom setups or troubleshooting, here are the key options:
-
poolQueryViaFetch: Setting
poolQueryViaFetch
to true sendsPool.query()
calls as low-latencyHTTP
fetch requests (currently defaults to false). -
wsProxy: This option is for connecting via a WebSocket proxy deployed in front of your 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.
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.