Lifecycle cleanup
Close a WebSocket-backed client after a query or transaction completes.
Neon · Module
Client.end() closes a Neon serverless driver's Client connection. Call it during cleanup after WebSocket-based queries, sessions, or interactive transactions finish.
Client.end() closes the Neon serverless driver's WebSocket-backed client connection. Use it after the client is no longer needed, typically in cleanup code after queries or interactive transactions have completed.
await client.end();A promise that resolves when the client connection has been closed.
pg ecosystem.import { Client } from '@neondatabase/serverless';
const client = new Client(process.env.DATABASE_URL);
await client.connect();
try {
const result = await client.query('select 1 as ok');
console.log(result.rows);
} finally {
await client.end();
}Connects a WebSocket Client, runs a query, and closes the client in a finally block.
Close a WebSocket-backed client after a query or transaction completes.
Release a client after using session state across multiple operations.
Ensure cleanup runs after success or failure with a finally block.
Client.end() terminates the client connection after work is complete. A typical pattern is to call it in a finally block so cleanup occurs whether the query succeeds or fails.
Follow-up: Why might the Client be unnecessary for a single query? The HTTP driver is designed for independent one-shot queries, while Client is for sessions, interactive transactions, or pg compatibility.
Neon's Client constructor uses the serverless driver's WebSocket transport. This provides session and transaction support and is compatible with the node-postgres API.
Follow-up: When should WebSockets be selected over HTTP? Select WebSockets for multi-step interactive transactions or sessions; select HTTP for independent queries.
The client should be closed after its final operation, commonly with await client.end() in cleanup code. This prevents the WebSocket-backed session from remaining open after application work finishes.
Follow-up: What alternative supports connection pooling? The Neon driver also provides Pool, which uses the same WebSocket-based mode for session and transaction scenarios.
PoolClientneon()WebSocketsinteractive transactionsnode-postgres compatibility