Registry / testing / postgres-cleaner

postgres-cleaner

JSON →
library1.0.6jsnpmunverified

A lightweight Node.js library for cleaning PostgreSQL databases during testing. Current version 1.0.6. Provides simple truncate or delete operations to clear all tables and reset sequences. Key differentiator: minimal API for test teardowns, supports skipping specific tables. Released as needed, maintenance mode.

npm install postgres-cleaner
INSTALL
IMPORT
SIG · POSTGRES-CLEANER
P
postgres-cleaner
testingjavascriptv1.0.6
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.

postgresCleaner
import cleaner from 'postgres-cleaner'
const cleaner = require('postgres-cleaner')
ESM is not supported; only CommonJS require works.
cleaner
const cleaner = require('postgres-cleaner')
import { cleaner } from 'postgres-cleaner'
The package exports a single function as default, not as named export.
Type usage
// No TypeScript types provided; use @types/postgres-cleaner if available
import { CleanerOptions } from 'postgres-cleaner'
This package does not ship TypeScript definitions.

Shows how to clean a PostgreSQL database using truncate with a skipped table.

const cleaner = require('postgres-cleaner'); const { Pool } = require('pg'); const pool = new Pool({ connectionString: process.env.DATABASE_URL ?? 'postgres://user:pass@localhost/testdb' }); const options = { type: 'truncate', skipTables: ['migrations'] }; cleaner(options, pool) .then(() => { console.log('Database cleaned successfully'); process.exit(0); }) .catch(err => { console.error('Database cleanup failed:', err); process.exit(1); });
Debug
Known issues
gotchaThe cleaner function does not close the database connection pool; you must manage pool lifecycle manually.
fix
Call pool.end() after cleaner completes if you want to release resources.
affects: >=1.0.0
gotchaPassing a pg-promise database object directly will fail due to compatibility; you must use the underlying pg Pool via pgp.pg.Pool.
fix
Create a Pool from pg-promise's pg module: const pool = pgp.pg.Pool({connectionString: ...})
affects: >=1.0.0
gotchaOptions.skipTables is optional but if misspelled (e.g., 'skipTable') it will be ignored, potentially dropping important tables.
fix
Ensure property name is exactly 'skipTables' as an array of strings.
affects: >=1.0.0
gotchadelete type does not reset sequences in some PostgreSQL versions; truncate is recommended for full cleanup.
fix
Use type: 'truncate' (default) for reliable sequence reset.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cleaner is not a function
Importing as named export instead of default.
fix
Use const cleaner = require('postgres-cleaner');
error: relation "tablename" does not exist
Querying a table after cleanup when the table was dropped or truncated in a transaction that was rolled back.
fix
Ensure cleaner runs outside a transaction that might rollback.
Cannot read property 'query' of undefined
Passing a non-pg Pool object (e.g., pg-promise db object) to cleaner.
fix
Extract the underlying pg Pool: const pool = pgp.pg.Pool({...});
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
postgres-cleaner — npm install postgres-cleaner · libregistry