Session-based workflows
Run several dependent statements that must share one database session.
Neon · Class
Neon’s Client is a WebSocket PostgreSQL client for JavaScript and TypeScript. Use it when an application needs a session, an interactive transaction, or node-postgres compatibility.
The Client constructor from @neondatabase/serverless uses the WebSocket transport and follows the node-postgres API. Connect it before issuing queries, then end it when finished; this mode is intended for sessions, interactive transactions, or node-postgres compatibility.
new Client(config)| Name | Type | Description |
|---|---|---|
config | string | ClientConfig | A Neon Postgres connection string, normally supplied through an environment variable. |
A WebSocket-based PostgreSQL client instance exposing the node-postgres-compatible client API.
neon() function is the documented choice.import { Client } from '@neondatabase/serverless';
const client = new Client(process.env.DATABASE_URL);
await client.connect();
try {
const result = await client.query('SELECT NOW() AS current_time');
console.log(result.rows[0]);
} finally {
await client.end();
}Creates a WebSocket Client, connects, runs a query, and closes the connection.
Run several dependent statements that must share one database session.
Execute a multi-step transaction whose statements depend on earlier results.
Adapt code or libraries built around the node-postgres client interface.
Client used for?Client is a WebSocket-based constructor in @neondatabase/serverless. It provides session and transaction support and is compatible with the node-postgres API.
Follow-up: When should HTTP be preferred? Use the neon() function for single queries and non-interactive transactions because those do not require a persistent session.
Instantiate Client with the database connection string, call connect(), execute queries through query(), and call end() when the session is complete. This follows the WebSocket client pattern described for the driver.
Follow-up: Why must cleanup be explicit? The WebSocket session remains active until it is closed, so ending the client prevents an unused connection from remaining open.
Neon’s Client is intended for WebSocket usage, while the driver’s HTTP mode is intended for single queries and non-interactive transactions. The choice depends on whether the application needs a session or interactive transaction.
Follow-up: What compatibility benefit does Client provide? Its API is compatible with node-postgres, helping applications and tools that expect that client interface use the Neon driver.
Poolneon()transaction()neonConfignode-postgres