Registry / database / pg-range

pg-range

JSON →
library1.1.2jsnpmunverified

pg-range version 1.1.2 is a plugin for node-postgres that adds automatic parsing and serialization of PostgreSQL range types (int4range, int8range, numrange, tsrange, tstzrange, daterange). It integrates with the pg library via install method and provides a Range class extending stRange.js for operations like contains, intersects. Released under MIT, last updated in 2016, stable but unmaintained. Differentiator: seamless integration with pg, no need for manual type handlers.

npm install pg-range
INSTALL
IMPORT
SIG · PG-RANGE
P
pg-range
databasejavascriptv1.1.2
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.

install
const { install } = require('pg-range'); install(pg);
import pgRange from 'pg-range'; pgRange.install(pg); // ESM not supported
pg-range uses CommonJS only; no ES module. Since v1, install must be called synchronously after pg initialised.
Range
const { Range } = require('pg-range');
const Range = require('pg-range').Range;
Both work, but destructuring is cleaner.
Range
import { Range } from 'pg-range';
import Range from 'pg-range'; // Wrong: pg-range does not have a default export
TypeScript users can use named import, but runtime is CommonJS.

Shows installation, querying a range column, and inserting a range value using pg-range with node-postgres.

const pg = require('pg'); const { install, Range } = require('pg-range'); install(pg); const client = new pg.Client(); client.connect(); // Query that returns a range client.query("SELECT int4range(1, 10) AS r", (err, res) => { if (err) throw err; console.log(res.rows[0].r.begin, res.rows[0].r.end, res.rows[0].r.bounds); client.end(); }); // Insert with a range client.query("INSERT INTO test (r) VALUES ($1)", [new Range(1, 10)]);
Debug
Known issues
gotchaDo not use raw stRange.js Range objects for queries; they lack toPostgres method, causing serialization errors for date ranges and empty ranges.
fix
Always use the Range constructor from pg-range itself, e.g., new Range(1, 3).
affects: <2.0.0
gotchainstall() must be called on the same pg instance that runs queries; if you have multiple pg instances, install on each.
fix
Call require('pg-range').install(pg) on the pg module you use.
affects: >=1.0.0
deprecatedpg-range has not been updated since 2016; no support for newer pg versions (≥8) or ESM. Possible incompatibilities with modern pg.
fix
Consider migrating to pg-types or custom type parsers if issues arise with newer pg versions.
affects: <1.2.0
gotchaRange bounds are output as string in 'bounds' field, not as enum; incorrectly assuming 'bound' may break logic.
fix
Check bounds using string comparison for characters: '[]', '[)', '(]', '()'.
affects: >=1.0.0
Errors
Common errors & fixes
Error: install is not a function
Importing pg-range incorrectly, e.g., using default import or destructuring wrong.
fix
Use const { install } = require('pg-range'); or const install = require('pg-range').install;
TypeError: Range is not a constructor
Using raw stRange.js Range or wrong import (e.g., import Range from 'pg-range').
fix
Use const { Range } = require('pg-range'); and call new Range(...).
Cannot find module 'pg-range'
Package not installed or ESM context trying dynamic require.
fix
Run npm install pg-range and ensure CommonJS environment (e.g., use require, not import).
Error: typeInt64 is not supported by this build of pg.js
Incompatibility with newer pg versions that changed type handling.
fix
Downgrade pg to version 7 or implement a custom type parser.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
pgrequiredpg-range is a plugin that installs into pg type system
strangerequiredProvides Range class with methods like contains, intersects
Agent activity
4 hits · last 30 days
node
4
Resources
pg-range — npm install pg-range · libregistry