Registry / database / pg-jobs

pg-jobs

JSON →
library4.4.2jsnpmunverified

A simple yet powerful PostgreSQL-backed job queue for Node.js (version 4.4.2). It provides persistent job creation, scheduled processing, and immediate processing with locking. Unlike alternative libraries (e.g., Kue, Bull) that require Redis, pg-jobs leverages PostgreSQL for storage and atomic operations, making it ideal for projects already using PostgreSQL. The library has a low release cadence but is stable for production use. Key features: job scheduling with delays, concurrent worker support, and automatic table creation via migrations.

npm install pg-jobs
INSTALL
IMPORT
SIG · PG-JOBS
P
pg-jobs
databasejavascriptv4.4.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.

default
✓ import jobQueue from 'pg-jobs'
✗ import { jobQueue } from 'pg-jobs'
Default export: the function that initializes the queue with connection options.
create
✓ import jobQueue from 'pg-jobs'; const jobs = jobQueue({ db: 'postgres://...' }); jobs.create(data, delay, done)
✗ jobs.create(data, done, delay)
Method on the queue instance. The second argument is processIn delay in milliseconds, not a callback.
process
✓ import jobQueue from 'pg-jobs'; const jobs = jobQueue({ db: 'postgres://...' }); jobs.process(worker, done)
✗ jobs.process(worker) without done callback
process() requires a done callback for error handling. It iterates jobs synchronously.

Demonstrates creating a new job with a delay, defining a worker function with done callback, and starting the processing loop.

import jobQueue from 'pg-jobs'; const jobs = jobQueue({ db: process.env.DATABASE_URL ?? 'postgres://localhost/mydb' }); // Create a job that will be processed after 2 seconds jobs.create({ type: 'email', to: 'user@example.com', subject: 'Hello' }, 2000, (err) => { if (err) console.error('Failed to create job', err); else console.log('Job created'); }); // Worker function to process jobs function worker(id, jobData, done) { console.log('Processing job', id, jobData); // Simulate work done(null, jobData, 1000); // Re-process after 1 second } // Start processing jobs jobs.process(worker, (err) => { if (err) console.error('Processor error', err); });
Debug
Known issues
breakingpg-jobs v4 drops support for Node.js < 10 and requires pg v8+. If you use older Node or pg, upgrade.
fix
Update Node.js to >=10 and pg to >=8.
affects: <4.0.0
gotchaThe 'processIn' argument in jobs.create() is in milliseconds. Passing null means the job will never be serviced again, not immediate processing.
fix
Use 0 for immediate processing or omit/use null only if you don't want retries.
affects: >=0.0.0
gotchajobs.process() runs synchronously; calling it multiple times from the same process will process jobs sequentially, not in parallel. To run workers concurrently, spawn multiple processes.
fix
Use process managers (e.g., cluster) to run multiple worker processes.
affects: >=0.0.0
deprecatedjobs.stopProcessing() is deprecated in v4.4. Use jobs.stop() instead.
fix
Replace jobs.stopProcessing() with jobs.stop() in your code.
affects: >=4.4.0
Errors
Common errors & fixes
Error: connect ECONNREFUSED 127.0.0.1:5432
PostgreSQL server is not running or connection string is wrong.
fix
Ensure PostgreSQL is running and DATABASE_URL is correct.
TypeError: jobs.create is not a function
Trying to use jobs.create on the imported module instead of on an initialized instance.
fix
Call jobQueue(options) first: const jobs = jobQueue(options); then jobs.create(...).
Error: relation "node_pg_jobs_dev" does not exist
Migrations have not been run for the required table.
fix
Run migrations: db-migrate up -m node_modules/pg-jobs/migrations/ --config database.json
Upgrade
Version history
4.4.2latest on npm
Audit
Dependencies
pgrequiredPostgreSQL client for Node.js, required for database operations.
Agent activity
7 hits · last 30 days
node
6
Resources
pg-jobs — npm install pg-jobs · libregistry