Registry / database / pg-db
library2.0.0jsnpmunverified

pg-db v2.0.0 is a helper module for node-postgres that adds transaction management, named parameter support, a simplified query API, and event hooks. It automatically returns connections to the pool and destroys connections on SQL errors. Transactions are implemented using Node.js domains, allowing different parts of an application to participate without manual context passing. Named parameters use :param syntax instead of $N. The package requires pg >= 7 as a peer dependency. No frequent releases; last update was years ago.

npm install pg-db
INSTALL
IMPORT
SIG · PG-DB
P
pg-db
databasejavascriptv2.0.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.

db
var db = require('pg-db')();
const db = require('pg-db');
Call require() as a function with no arguments to create a database instance. Do not forget the parentheses.
db.query
db.query('SELECT * FROM t', function(err, rows) { ... })
db.query('SELECT * FROM t').then(rows => ...)
This library uses callbacks, not promises. No .then() or async/await support.
db.tx
var async = require('async'); db.tx.series([...], cb);
db.tx(function(cb) { ... })
db.tx is an object with series, parallel, and waterfall methods from async. Not a function itself.

Shows creating a default database instance, executing a simple query with callback, and using named parameters.

var db = require('pg-db')(); db.query('SELECT foo, bar, baz FROM some_table', function(err, rows){ if( err ) return console.error('Err:', err); console.log('Rows:', rows); }); // Named parameters example: db.queryOne('SELECT * FROM some_table WHERE foo = :foo', {foo: 123}, function(err, row){ if( err ) return console.error('Err:', err); console.log('Row:', row); });
Debug
Known issues
breakingTransactions use Node.js domains, which are deprecated since Node v12 and may be removed in future Node versions.
fix
Consider migrating to modern transaction patterns using pg's native transaction support or async/await with pg-promise.
affects: >=0.10.0
gotchaAlways call require('pg-db')() with parentheses. Omitting them returns the constructor, not a db instance, leading to 'db.query is not a function'.
fix
Use var db = require('pg-db')(); instead of var db = require('pg-db');
affects: >=0.1.0
gotchaThe library uses callbacks only; .then() or async/await will not work.
fix
Use callbacks or wrap with util.promisify() for promise support.
affects: >=0.1.0
breakingNamed parameters use :param syntax, not $N. Using $N directly will be treated as literal text.
fix
Use :paramName in SQL and supply an object with matching keys.
affects: >=2.0.0
deprecatedThe 'events' API may be unstable; no clear documentation on version support.
fix
Check the source or avoid using events in production until clarified.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: db.query is not a function
require('pg-db') returns the constructor, not an instance.
fix
Use var db = require('pg-db')(); (with parentheses).
Error: connect ECONNREFUSED 127.0.0.1:5432
No PostgreSQL server running or invalid DATABASE_URL.
fix
Set process.env.DATABASE_URL to a valid connection string, e.g., 'postgres://user:pass@localhost:5432/mydb'.
TypeError: Cannot read property 'query' of undefined
db instance not created correctly (missing parentheses).
fix
Ensure var db = require('pg-db')(); is used.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
pgrequiredpeer dependency required for database connectivity
Agent activity
5 hits · last 30 days
node
4
Resources
packagepg-db
pg-db — npm install pg-db · libregistry