Close the pool before the request ends
Request-scoped edge handler
Neon · Module
Pool.end() closes a Neon serverless driver's WebSocket pool after its work is complete. Await it during cleanup, especially when the pool is created within a request handler.
Pool.end() closes the Neon serverless driver's WebSocket pool and should be awaited after the pool's work is complete. In request-scoped runtimes, create and close the pool within the same request so the connection does not outlive the handler.
await pool.end();A promise that resolves when the pool has been closed.
import { Pool } from '@neondatabase/serverless';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const { rows } = await pool.query('SELECT 1');
await pool.end();
console.log(rows);Creates a WebSocket-backed pool, runs a query, and closes the pool when finished.
Request-scoped edge handler
Node.js service using Pool
Interactive transaction or session
Pool.end() shuts down the pool after its queries have completed. Awaiting it ensures cleanup finishes before the surrounding operation ends.
Follow-up: Why might Pool be unsuitable for a single stateless query? The HTTP query interface avoids managing a persistent pool, while Pool is intended for sessions, interactive transactions, or node-postgres compatibility.
Neon's Pool uses WebSockets and is compatible with the node-postgres Pool API. Calling end closes that pool's resources after use.
Follow-up: When should a pool be created per request? In edge runtimes where WebSocket connections cannot outlive a request, create, use, and close it inside the handler.
The pool should be closed after the final query or transaction has completed, typically with await pool.end(). This prevents a request-scoped WebSocket pool from remaining open.
Follow-up: What alternative fits a one-statement, stateless operation? Use the driver's HTTP-based neon function rather than a WebSocket Pool.
PoolClientneon()@neondatabase/serverless