pg_dump vs Logical Replica vs Snapshot Restore: How to Get Production Data Into PostgreSQL Staging (2026)

Every team that runs PostgreSQL eventually wants staging to look like production. There are three common ways to get there: a pg_dump of production restored into staging, a logical replica that streams changes from production, or a storage snapshot (for example an Amazon RDS snapshot) restored as a new instance. This guide compares the three on freshness, cost, isolation and, most importantly, what each one does with personal data.

The short answer

pg_dump + restore Logical replica Snapshot restore
Freshness As of the dump (daily/nightly is typical) Near real time As of the snapshot
Schema copied Yes No (DDL is not replicated) Yes (whole instance)
Staging writable Yes Yes, but writes can conflict with incoming changes Yes
Copy only some tables Yes Yes (per publication) No, all or nothing
Restore time on big databases Slowest Initial sync, then continuous Usually fastest
Can remove PII on the way Yes, by transforming the dump Hard: every change carries real data No: you copy real data first, then mask

If staging is used by developers, contractors or CI, the deciding row is the last one. The rest of this article explains why.

Option 1: pg_dump and restore

pg_dump exports a database as SQL (or a custom archive) that you restore with psql or pg_restore. The PostgreSQL documentation notes that it makes consistent exports even while the database is in use, and it does not block other readers or writers (pg_dump docs).

Good for: small to medium databases, nightly refreshes, copying a subset of tables, and moving data between providers or major versions.

Downsides: it is a point-in-time copy, and dumping and restoring a large database takes time. A plain dump also contains every email, phone number and address in production, so the file becomes sensitive the moment it is written. You can see this in the guide to anonymizing a pg_dump.

The upside is that a dump is a stream you control. You can replace PII while it is being written, so the raw data never lands on disk or in staging.

Option 2: a logical replica

Logical replication sends row changes from a publication on production to a subscription on another PostgreSQL server. Staging stays close to real time and you can publish only some tables.

The PostgreSQL docs on restrictions list the trade-offs. Two matter most for staging:

  • The schema and DDL are not replicated. You create the tables on staging yourself and apply every migration on both sides. If you forget, replication can stop.
  • Sequence data is not replicated in current releases, so after a cutover or a test run, sequences on staging may be behind the real data.

A replica also needs wal_level = logical on production, a replication slot, and monitoring so the slot does not keep WAL piling up if staging goes offline. And because every change arrives as it happened in production, every customer email arrives too. Masking a live stream is possible (for example with triggers on the subscriber), but it is fragile and easy to bypass. A physical streaming replica is not an option for staging either: a hot standby only accepts read-only queries (hot standby docs).

Good for: reporting replicas, migrations between servers, and near-real-time read copies inside the same trust boundary as production. Not great for: developer staging, because it copies live PII continuously.

Option 3: restore a storage snapshot

Managed services let you snapshot the whole instance and restore it as a new one. On Amazon RDS, for example, restoring a DB snapshot creates a new DB instance (RDS docs). On very large databases this is often the fastest way to get a full copy, because nothing is replayed row by row.

Downsides: it is all or nothing (every database, table and role on the instance), it usually only works inside the same provider and account, and the new instance is a full-size server you pay for. Most importantly, the restored copy holds real production data. The usual fix is to restore it in a locked-down network, run masking UPDATEs, and only then open it to developers. If that masking step fails halfway, you have a staging server full of real personal data.

A cleaner pattern is to use the snapshot only as a source: restore it privately, take an anonymized dump from it, give that dump to staging, then delete the restored instance. Production gets no extra load and staging never sees real data.

Which one should you pick?

  • Developer and QA staging, CI, contractors: pg_dump with anonymization on the way out. Staging gets a realistic, restorable copy and never holds real PII. This fits GDPR's data minimisation idea well (see GDPR and test data).
  • Read-only reporting next to production: a logical or physical replica, kept inside the production security boundary.
  • Huge databases where dumping production directly is too slow or too heavy: restore a snapshot privately, take an anonymized dump from that restore, then delete it.

Anonymized dumps with Cloud-DB-Anonymizer

Cloud-DB-Anonymizer is a free, MIT-licensed CLI that does the pg_dump route with PII removed. It reads from a read-only REPEATABLE READ snapshot, uses pg_dump for the schema, writes data in foreign-key order with sequences and identity columns, and replaces PII with deterministic fakes seeded by HMAC-SHA256. The same input with the same salt always gives the same fake, so joins across tables still work.

git clone https://github.com/fadadyounes-pixel/cloud-db-anonymizer
cd cloud-db-anonymizer && pip install -r requirements.txt

python main.py init rules.json          # starter rules file
python main.py strategies               # list the 20 strategies
python main.py preview jane@corp.com --strategy email

export ANON_SALT='a-long-random-secret'
python main.py sanitize --db-url "$PROD_READONLY_URL" --rules rules.json --dry-run
python main.py sanitize --db-url "$PROD_READONLY_URL" --rules rules.json -o staging.sql.gz

gunzip -c staging.sql.gz | psql "$STAGING_DB_URL"

A minimal rules.json:

{
  "schema": "public",
  "locale": "en_US",
  "include_unlisted_tables": true,
  "exclude_tables": [],
  "tables": {
    "users": {"columns": {"email": "email", "full_name": "name", "phone": "phone"}},
    "payments": {"columns": {"card_number": "credit_card", "iban": "iban"}}
  }
}

For the snapshot pattern, point --db-url at the privately restored instance instead of production. To keep a copy in object storage, add --upload my-bucket (and --endpoint-url for Cloudflare R2, DigitalOcean Spaces or MinIO).

If you want this on a schedule without writing cron jobs, the Pro Suite is a self-hosted Docker Compose stack with a web dashboard, a rules editor, a cron scheduler, parallel uploads to AWS S3, Cloudflare R2 and DigitalOcean Spaces with retention pruning, and Telegram or Discord alerts. Agencies running it for several clients can use the Agency License.

Summary

Replicas are best for freshness, snapshots for speed, and dumps for control. For any staging environment that people outside the production team can reach, control wins: take a dump, remove the PII while writing it, and restore only the clean copy. More guides are on the PostgreSQL PII anonymizer page.