Registry / database / pg-query

pg-query

JSON →
library0.11.0jsnpmunverified

pg-query is a lightweight wrapper around node-postgres that reduces boilerplate for PostgreSQL queries. Version 0.11.0 is the latest stable release, though the package is in maintenance mode with no active development. Key differentiators: 3-argument callback (err, rows, result), promise support via .spread(), query.first helper for single-row results, and domain-aware error handling. It uses a random pooled client per query, so it is not suitable for transactions. The package has no external dependencies beyond node-postgres (pg) and is designed for simple query operations.

npm install pg-query
INSTALL
IMPORT
SIG · PG-QUERY
P
pg-query
databasejavascriptv0.11.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ var query = require('pg-query');
✗ import query from 'pg-query';
CJS-only module; no ESM or default export. Use require.
query
✓ const query = require('pg-query');
✗ const { query } = require('pg-query');
Direct callable returned; not an object with query method.
query.first
✓ query.first('SELECT * FROM users WHERE id = $1', id, callback);
✗ query.first('SELECT * FROM users WHERE id = $1', [id], callback);
query.first wraps non-array values in an array internally. You can pass a single value directly.

Initialize pg-query with connection string and run a simple SELECT query with callback.

var query = require('pg-query'); query.connectionParameters = process.env.DATABASE_URL || 'postgres://user:password@localhost:5432/mydb'; query('SELECT NOW()', function(err, rows, result) { if (err) { console.error('Query failed:', err); return; } console.log('Current time:', rows[0].now); });
Debug
Known issues
gotchapg-query uses a random pooled client per query, so transactions (BEGIN/COMMIT) will not work correctly.
fix
For transactions, use node-postgres directly with a client from pg.Client.
affects: >=0.0.0
deprecatedquery.spread() returns a promise that resolves with an array [rows, result]; this is non-standard and may confuse users.
fix
Use standard promise .then() with destructuring: query(sql).then(([rows, result]) => ...) or switch to a more modern library like postgres.js.
affects: >=0.0.0
gotchaquery.first() wraps non-array values in an array. Passing an array is passed as-is, which can lead to double-wrapping if you pass an array of single value.
fix
For a single value, pass the value directly; for multiple values, pass an array.
affects: >=0.0.0
breakingNo breaking changes documented; package is stable but unmaintained.
fix
Consider migrating to the newer pg package's built-in pool query or postgres.js.
affects: 0.11.0
Errors
Common errors & fixes
Error: Cannot find module 'pg-query'
The pg-query package is not installed or node_modules is missing.
fix
Run 'npm install pg-query' in your project directory.
TypeError: query is not a function
Trying to import pg-query using ES module syntax or destructuring wrong.
fix
Use 'const query = require('pg-query');' (CJS). Do not use import or destructure.
Error: connect ECONNREFUSED
PostgreSQL server is not running or connection parameters are wrong.
fix
Ensure PostgreSQL is started and connection string is correct: query.connectionParameters = 'postgres://user:pass@host:port/db';
Upgrade
Version history
0.11.0latest on npm
Audit
Dependencies
pgrequiredPeer dependency; pg-query uses whichever version of node-postgres is installed in the project.
Agent activity
9 hits · last 30 days
node
8
Resources
pg-query — npm install pg-query · libregistry