Every engineering team eventually wants real data in staging. Bugs reproduce faster, migrations get tested against real volumes, and demos look believable. The problem is that a straight pg_dump of production copies every customer's email, phone number and payment detail into an environment with weaker access controls. That is how a lot of data leaks start.
This guide shows how to produce a sanitized, restorable PostgreSQL dump where personal data is replaced with realistic fake values, and the data still behaves like production.
Why naive masking breaks your database
The obvious approach is an UPDATE script that sets every email to user123@example.com or fills columns with random strings. That usually fails in three ways:
-
Unique constraints collide. If two customers both become
test@example.com, the restore fails on the unique index. -
Joins stop matching. If
users.emailandorders.customer_emailare randomized independently, they no longer refer to the same person. -
You masked production itself. Running
UPDATEon a copy is fine. Running it on the wrong connection string is a very bad day.
The fix: deterministic anonymization
Deterministic anonymization means the same real value always becomes the same fake value. Internally, each value is hashed with HMAC-SHA256 using a secret salt, and that hash seeds a fake-data generator. So jane.doe@acme.com becomes, say, antoniomacdonald.faa812c4@example.com in every table and on every run.
That gives you three properties at once:
- Joins and foreign keys keep working, because matching values stay matching.
- Unique columns stay unique, because different inputs produce different outputs.
- Without the salt, nobody can reverse or reproduce the mapping. Rotate the salt to get a completely new data set.
Step 1: Install the free CLI
Cloud-DB-Anonymizer is a free, MIT-licensed Python tool that does exactly this. It needs Python 3.10+ and, ideally, the PostgreSQL client tools so the dump includes your schema.
git clone https://github.com/fadadyounes-pixel/cloud-db-anonymizer.git
cd cloud-db-anonymizer
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
Step 2: Create a secret salt
export ANON_SALT=$(openssl rand -hex 32)
Keep this value secret and stable. The same salt produces the same fake data every time, which makes nightly dumps consistent.
Step 3: Describe which columns hold personal data
Create a rules.json that maps each sensitive column to a strategy:
{
"schema": "public",
"exclude_tables": ["audit_logs", "sessions"],
"tables": {
"users": { "columns": { "email": "email", "full_name": "name", "phone": "phone" } },
"payments": { "columns": { "card_number": "credit_card", "iban": "iban" } },
"support_tickets": { "columns": { "body": "redact" } }
}
}
There are 20 strategies, including email, name, phone, credit_card (Luhn-valid test numbers), iban, ssn, address, ip, date_of_birth, hash, redact and null. Use exclude_tables to drop tables entirely, and skip_data to keep only the schema of huge log tables.
Step 4: Check your rules against the live schema
python main.py sanitize --db-url "$DATABASE_URL" --rules rules.json --dry-run
The dry run connects read-only, prints which tables and columns will be anonymized, and warns about rules that point at renamed or missing columns. Running this in CI is a cheap way to catch new PII columns before they leak.
Step 5: Export the sanitized dump
python main.py sanitize --db-url "$DATABASE_URL" --rules rules.json
The tool opens a single REPEATABLE READ, READ ONLY transaction, so the export is a consistent snapshot and production is never modified. It exports the schema with pg_dump, streams rows with server-side cursors, writes data in foreign-key order, resets sequences and handles identity, JSONB and array columns. The result is a gzip file such as sanitized_production_20261029T031500Z.sql.gz.
Step 6: Restore into staging
gunzip -c sanitized_production_*.sql.gz | psql "$STAGING_DATABASE_URL"
What the data looks like afterwards
| Column | Production | Staging |
|---|---|---|
| jane.doe@acme.com | antoniomacdonald.faa812c4@example.com | |
| name | Jane Doe | Maria Harmon |
| phone | +212 600-111222 | +1-555-726-7654 |
| card | 4111 1111 1111 1111 | 4756410247684656 |
Automating it
Once the dump works by hand, the next step is running it nightly and storing the result somewhere your team can restore from. The free CLI can upload to one S3-compatible bucket with --upload. If you want scheduled runs, parallel upload to AWS S3, Cloudflare R2 and DigitalOcean Spaces, a web dashboard and Telegram or Discord alerts, the Pro Suite packages all of that as a self-hosted Docker Compose app.
Checklist
- Use a read-only database user for exports.
- Keep
ANON_SALTin a secrets manager, never in the repo. - Review
rules.jsonwhenever a migration adds a column. - Exclude tables you never need in staging, such as sessions and audit logs.
- Remember that anonymization supports GDPR data minimization, but your rules must cover every personal-data column.
Download the free CLI or read the project page.