Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Neon · Const

Vercel Postgres

Neon's serverless driver replaces the Vercel Postgres SDK with a client for serverless and edge applications. Its HTTP API handles one-shot queries, while WebSocket-based classes support sessions and interactive transactions.

How it works

The Neon serverless driver exposes a neon() function that turns a database URL into a query client. Its HTTP transport is suited to one-shot queries and non-interactive transactions; use Pool or Client over WebSockets when the application needs sessions or interactive transactions.

Syntax

neon(connectionString)

Parameters

NameTypeDescription
connectionStringstringA Neon PostgreSQL connection string, normally supplied through the DATABASE_URL environment variable.

Returns

The neon() factory returns a tagged SQL query function whose execution resolves to the query result rows.

Key points

  • Install the package with npm install @neondatabase/serverless before importing its client.
  • Initialize the SQL client with the connection string stored in DATABASE_URL.
  • The driver supports two connection styles, selected according to whether the workload is stateless or session-based.
  • Use the tagged SQL client for independent statements and transactions that do not require an interactive session.
  • Choose the WebSocket classes when an application needs persistent sessions, interactive transactions, or node-postgres compatibility.

Common mistakes

  • HTTP is not the right transport for interactive transactions or session-oriented workloads; use the WebSocket-based Pool or Client instead.
  • Use this driver in edge environments where ordinary TCP-based PostgreSQL clients cannot establish connections.

Example: Run a query with the Neon serverless driver

import { neon } from "@neondatabase/serverless";

const sql = neon(process.env.DATABASE_URL!);
const rows = await sql`SELECT now() AS current_time`;
console.log(rows[0].current_time);

Creates an HTTP-based Neon SQL client from the database URL and runs a parameter-safe tagged-template query.

When to use it

One-shot serverless queries

Run independent SQL statements from serverless functions or edge handlers without managing TCP connections.

Stateless transactional work

Execute non-interactive transactions through the HTTP transport.

Interactive database workflows

Maintain sessions or perform multi-step interactive transactions with WebSockets.

Vercel migration

Reuse a Neon-compatible client across Vercel and other runtimes that support the package.

Interview questions

QHow does the Neon serverless driver connect to Postgres?

The neon() function creates a client from DATABASE_URL, and tagged-template calls execute SQL over HTTP. This avoids requiring a TCP PostgreSQL connection for each function invocation.

Follow-up: How would you support an interactive transaction? Use the package's WebSocket-based Pool or Client rather than the HTTP query function.

QWhen should the HTTP transport be used?

HTTP is intended for individual queries and non-interactive transactions. It is a good fit for short-lived serverless or edge requests because each query is sent as a fetch request.

Follow-up: What is the trade-off? HTTP does not provide the persistent session semantics needed by interactive, multi-step transactions.

QWhen should Pool or Client be used instead?

Use Pool or Client when the application needs sessions, interactive transactions, or compatibility with the node-postgres API. These classes communicate over WebSockets.

Follow-up: Why not use neon() for that case? Its HTTP request model is designed for independent query operations, not a stateful session.

  • Pool
  • Client
  • DATABASE_URL
  • Vercel-Managed integration
  • WebSockets
  • HTTP

Source: Migrate from Vercel Postgres SDK to the Neon serverless driver. Checked 2026-10-16.