Registry / database / pg-ears

pg-ears

JSON →
library1.2.3jsnpmunverified

Resilient Postgres LISTEN/NOTIFY client with automatic reconnection. Current stable version: 1.2.3. Low release cadence (no recent updates). The author recommends using pg-listen instead. Handles connection drops, but has known issue of accumulating LISTEN statements on reconnect. Differentiator: built-in reconnect with configurable check interval and max attempts.

npm install pg-ears
INSTALL
IMPORT
SIG · PG-EARS
P
pg-ears
databasejavascriptv1.2.3
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 (factory function)
const pgEars = require('pg-ears')(options)
import pgEars from 'pg-ears'
CommonJS only; no ESM export. Must call the exported function with options.
listen
pgEars.listen('channel', (err, data) => {})
pgEars.listen('channel', data => {})
Callback signature with error-first pattern; first argument is error, second is data.
notify
pgEars.notify('channel', payload, (err) => {})
pgEars.notify('channel', payload)
Optional callback; without callback, errors are silently lost.

Connect to Postgres, listen on a channel, and notify with JSON payload.

const pgEars = require('pg-ears')({ user: process.env.PGUSER ?? '', database: process.env.PGDATABASE ?? '', password: process.env.PGPASSWORD ?? '', host: process.env.PGHOST ?? 'localhost', port: Number(process.env.PGPORT) || 5432 }) pgEars.listen('mychannel', (err, data) => { if (err) return console.error(err) console.log(JSON.stringify(data)) }) pgEars.notify('mychannel', {key: 'value'}, (err) => { if (err) console.error(err) })
Debug
Known issues
deprecatedAuthor recommends using pg-listen instead of pg-ears
fix
Switch to pg-listen (https://github.com/andywer/pg-listen)
affects: >=0.0.0
gotchaLISTEN statements leak on reconnect; each reconnect adds another LISTEN without removing old ones
fix
Manually manage reconnection or switch to pg-listen
affects: >=0.0.0
gotchaChannel names are not sanitized; SQL injection possible if using dynamic channel names
fix
Ensure channel names are safe PostgreSQL identifiers; do not use user input directly
affects: >=0.0.0
gotchaNo ESM export; module uses CommonJS only
fix
Use require() instead of import
affects: >=0.0.0
gotchaCallback for listen expects error-first (err, data); forgetting error argument causes silent failures
fix
Always use (err, data) => {...} signature
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: pgEars is not a function
Calling require('pg-ears') instead of require('pg-ears')(options)
fix
Invoke the exported function: const pgEars = require('pg-ears')(options)
Error: listen statement accumulated
Reconnection causes duplicate LISTEN statements; known issue #6
fix
Avoid using pg-ears; use pg-listen instead
Cannot find module 'pg'
pg is a peer dependency not automatically installed
fix
npm install pg pg-ears
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies
pgrequirednode-postgres is required for database connection
Agent activity
5 hits · last 30 days
node
4
Resources
pg-ears — npm install pg-ears · libregistry