One-shot HTTP queries
A serverless function needs to run a single SQL query without maintaining a database session.
Neon · Const
Neon’s serverless driver lets JavaScript and TypeScript applications query Postgres from serverless and edge runtimes over HTTP or WebSockets. Use neon() over HTTP by default, reserving Pool or Client for sessions, interactive transactions, or pg compatibility.
The Neon serverless driver is a JavaScript and TypeScript Postgres driver for serverless and edge environments. Its neon() function sends queries over HTTP, while Pool and Client use WebSockets for interactive transactions, sessions, or compatibility with node-postgres.
neon(connectionString)| Name | Type | Description |
|---|---|---|
connectionString | string | Neon PostgreSQL connection string used to configure the client. |
A query client function from neon() that executes SQL over HTTP and resolves query results.
@neondatabase/serverless package is added to the project.import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
const posts = await sql`SELECT * FROM posts`;
console.log(posts);Creates an HTTP-based Neon SQL client from the connection string and runs a parameterized query.
A serverless function needs to run a single SQL query without maintaining a database session.
An edge application runs in an environment where direct TCP database connections are unsuitable.
An application requires session state or multiple coordinated statements in an interactive transaction.
The driver is a JavaScript and TypeScript Postgres driver designed for serverless and edge runtimes. It communicates with Neon over HTTP or WebSockets instead of TCP.
Follow-up: Which transport should be preferred for a single query? Use HTTP through the neon() function because it is the documented default for single queries and non-interactive transactions.
Call neon() with a Neon connection string, then execute a tagged-template query such as sql\SELECT ...\. The HTTP client is intended for single queries and non-interactive transactions.
Follow-up: How would the client change for an interactive transaction? Use Pool or Client over WebSockets, because those APIs maintain the state needed for interactive transactions.
Pool or Client be used instead of neon()?Use Pool or Client when the application needs interactive transactions, sessions, or compatibility with pg. For ordinary serverless queries, use neon() over HTTP instead.
Follow-up: Why is this distinction useful in edge environments? HTTP avoids requiring a TCP connection, matching runtimes that support HTTP-based database access but restrict traditional sockets.
neon()PoolClient@neondatabase/serverless