One-shot serverless queries
Run independent SQL statements from serverless functions or edge handlers without managing TCP connections.
Neon · Const
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.
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.
neon(connectionString)| Name | Type | Description |
|---|---|---|
connectionString | string | A Neon PostgreSQL connection string, normally supplied through the DATABASE_URL environment variable. |
The neon() factory returns a tagged SQL query function whose execution resolves to the query result rows.
npm install @neondatabase/serverless before importing its client.DATABASE_URL.node-postgres compatibility.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.
Run independent SQL statements from serverless functions or edge handlers without managing TCP connections.
Execute non-interactive transactions through the HTTP transport.
Maintain sessions or perform multi-step interactive transactions with WebSockets.
Reuse a Neon-compatible client across Vercel and other runtimes that support the package.
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.
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.
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.
PoolClientDATABASE_URLVercel-Managed integrationWebSocketsHTTP