Video in progress
How do you use prod data for test and staging environments: anonymization, subsets and keeping it fresh?
I don't copy a prod dump straight into staging. An automated pipeline deterministically anonymizes the PII in the copy, takes a subset without breaking FKs if needed, and runs regularly. The rules live in the same repo as the schema.
📖 Answer
Prod data is valuable for real testing: the real distribution, real edge cases, real volume. But access to staging is much wider and its logs are unprotected, so PII that lands there is effectively the same as leaked.
The pipeline I build:
- A temporary restore from the prod backup in an isolated environment.
- An anonymization script, with a rule for every PII column:
- name, phone, email: fake values with the right format;
- the same input → the same output (based on HMAC), so JOINs and duplicates are preserved;
- small noise on dates and amounts, so the distribution is preserved;
- free-text fields (comments, chat) are replaced entirely, because you can't know in advance what's in them.
- A subset if needed: for example 5% of customers and everything linked to them through FKs.
- The result is loaded into staging as a dump, and the temporary environment is deleted.
UPDATE customer
SET full_name = 'Mijoz ' || id,
phone = '+99890' || lpad((abs(hashtext(phone)) % 10000000)::text, 7, '0'),
email = 'user' || id || '@example.test';
An important rule: when a new column is added, there must be an anonymization rule for it too. I put a check in the migration CI: a new column with no rule that isn't marked pii gives a warning. Otherwise a year from now a new passport_no column quietly leaks into staging.
For most tests, a prod copy isn't needed at all; generated data is enough. I keep the prod copy only for load and plan checks.
Sign up to mark studied questions and save your progress.