pg-anonymizer
raw JSON → 0.8.1 verified Sat Apr 25 auth: no javascript
pg-anonymizer is a CLI tool (built with oclif) that exports a PostgreSQL database and anonymizes sensitive columns using faker.js. It wraps pg_dump and replaces data on the fly. Current stable version 0.8.1, released Feb 2024, with ~5 releases per year. Key differentiators: no database installation, works as a pipe, supports custom faker functions, extension files, and config files. Alternative to pg_dump with custom scripts or postgresql_anonymizer, but simpler and portable.
Common errors
error pg_dump: command not found ↓
cause pg_dump is not installed on the system.
fix
Install PostgreSQL client tools (e.g., on Ubuntu: sudo apt-get install postgresql-client).
error Error: Cannot find module 'pg-anonymizer' ↓
cause When using require in an older CJS project without transpilation, but package is ESM-only in some versions.
fix
Use dynamic import() or switch to ESM: 'import pgAnonymizer from "pg-anonymizer"'.
error Error: Column 'nonexistent' does not exist in any table ↓
cause Specified a column name that does not exist in any database table.
fix
Check the column name spelling and ensure it exists in the database.
Warnings
gotcha pg_dump must be installed separately; it is not bundled with npm package. ↓
fix Install PostgreSQL client tools (e.g., apt-get install postgresql-client) or ensure pg_dump is in PATH.
breaking v0.6.0 changed default faker from faker.random.word to auto-detection based on column name; custom --columns without faker function now auto-detects. ↓
fix If you relied on faker.random.word default, specify columns explicitly with ':faker.random.word'.
deprecated The --columns option with dot-notation for tables (e.g., public.user.email) may be deprecated in future; prefer using --skip for tables. ↓
fix Use --skip to exclude tables from anonymization instead of prefixing column names.
gotcha Unexpected anonymization of non-sensitive columns: if no --columns specified, a default list of columns (email, name, etc.) is anonymized, potentially including non-sensitive data. ↓
fix Always specify --columns to avoid accidentally anonymizing unintended columns.
gotcha Output file path is relative to current working directory, not the script location. ↓
fix Use absolute paths for --output to avoid confusion.
deprecated Node.js >=12 required; Node 10 support dropped in v0.6.0. ↓
fix Upgrade Node.js to >=12.
Install
npm install pg-anonymizer yarn add pg-anonymizer pnpm add pg-anonymizer Imports
- default wrong
const pgAnonymizer = require('pg-anonymizer')correctimport pgAnonymizer from 'pg-anonymizer' - anonymize wrong
const { anonymize } = require('pg-anonymizer')correctimport { anonymize } from 'pg-anonymizer' - types
import type { Config } from 'pg-anonymizer'
Quickstart
npx pg-anonymizer postgres://user:pass@localhost:5432/mydb --columns=email,firstName,lastName --output dump.sql