pg-query-stream is a Node.js library that provides a readable object stream interface for PostgreSQL query results, using server-side cursors to efficiently handle large result sets without consuming excessive memory. It integrates with the popular 'pg' library (version 8+) and supports TypeScript types. Version 4.15.0 is stable and actively maintained. Unlike manual pagination with LIMIT/OFFSET, it streams rows in real-time, making it ideal for ETL pipelines, data exports, or any scenario where processing large datasets row by row is required. It does not work with the native pg bindings.
npm install pg-query-streamNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to stream a large query result to stdout without blowing up memory.
Ensure you are using the JavaScript client by requiring 'pg' or 'pg.js' explicitly.
Always attach a data listener or pipe the stream. Use the 'end' event to release the client via the done callback.
Ensure your PostgreSQL version is 8.2+ and the connection does not use a connection-string parameter that disables cursors.
Use stream.destroy() or handle errors/early exits to close the stream properly.
Run: npm install pg-query-stream
Ensure you do not call client.query() more than once per stream. Reuse the same QueryStream instance.
ESM: import QueryStream from 'pg-query-stream'. CJS: const QueryStream = require('pg-query-stream').