Most teams eventually want realistic data in CI: integration tests that run against production-shaped rows, a staging database refreshed every night, or a migration rehearsed on real volumes before it hits production. The problem is that a raw pg_dump of production is full of emails, names, phone numbers and card data, and a CI runner is the last place you want that to land. This guide shows how to anonymize a PostgreSQL database inside GitHub Actions with the free, MIT-licensed Cloud-DB-Anonymizer CLI, so the only thing your pipeline ever stores is a sanitized, restorable dump.
What you will build
- A pull-request check that validates your anonymization rules (
rules.json) against a throwaway PostgreSQL service container, so a renamed column never silently leaks PII. - A scheduled workflow that connects to a read replica, writes a gzip SQL dump with every PII column replaced by deterministic fakes, and uploads it to a private S3-compatible bucket.
The CLI reads from a single REPEATABLE READ, read-only snapshot, so the dump is consistent even while the source is taking writes. Values are replaced with HMAC-SHA256-seeded fakes, which means the same real email always becomes the same fake email: joins, unique constraints and foreign keys keep working.
Step 1: Commit your rules file
Start from the starter file and edit it to match your schema:
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
A realistic rules file maps each sensitive column to one of the 20 built-in strategies (run python main.py strategies to see them all):
{
"schema": "public",
"locale": "en_US",
"include_unlisted_tables": true,
"exclude_tables": ["sessions", "audit_logs"],
"tables": {
"users": { "columns": { "email": "email", "full_name": "name", "phone": "phone", "last_login_ip": "ip" } },
"payments": { "columns": { "card_number": "credit_card", "iban": "iban" } },
"api_keys": { "columns": { "secret": "hash" } },
"uploads": { "skip_data": true }
}
}
Check what a value turns into without touching a database:
python main.py preview jane@corp.com --strategy email
Commit rules.json to the repository that owns the schema. Treat it like a migration: whenever someone adds a PII column, the rules change in the same pull request.
Step 2: Store secrets, not data
In Settings → Secrets and variables → Actions, add:
-
REPLICA_DATABASE_URL— a connection string for a read-only role on a replica, never the primary's superuser. -
ANON_SALT— a long random string (for exampleopenssl rand -hex 32). The CLI warns if you use the default salt; with a private salt nobody can recompute the fakes to match them back to real values. -
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYand, for Cloudflare R2, DigitalOcean Spaces or MinIO,S3_ENDPOINT_URL.
The CLI reads DATABASE_URL, ANON_SALT and S3_ENDPOINT_URL from the environment, so none of these values need to appear on the command line or in logs. Your runner also needs network access to the replica: either allow GitHub-hosted runner IPs through your firewall or, more commonly, use a self-hosted runner inside your VPC.
Step 3: Validate rules on every pull request
GitHub Actions can start PostgreSQL as a service container. Load your schema (no data) into it and run a dry run: --dry-run connects, checks every table and column in rules.json against the live schema, prints the export plan and exits without writing anything. A rule that points at a missing table or column is reported as a warning (“not found”) rather than an error, so the last step greps the output and fails the job if any appear.
name: anonymizer-rules
on: pull_request
jobs:
validate:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ["5432:5432"]
options: >-
--health-cmd pg_isready --health-interval 5s
--health-timeout 5s --health-retries 10
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: { python-version: "3.12" }
- run: git clone --depth 1 https://github.com/fadadyounes-pixel/cloud-db-anonymizer anonymizer
- run: pip install -r anonymizer/requirements.txt
- run: psql "$DATABASE_URL" -f db/schema.sql # your schema-only file or migrations
- run: |
COLUMNS=200 python anonymizer/main.py sanitize --rules rules.json --dry-run | tee plan.log
! grep -q "not found" plan.log
Read the plan in the job log: any table listed with no anonymized columns is worth a second look before you merge.
Step 4: Produce a sanitized dump on a schedule
The nightly job runs the real export against the replica and uploads the result. Cron schedules in GitHub Actions use UTC and run on the default branch; GitHub notes they can be delayed during periods of high load, so do not rely on exact minutes.
name: sanitized-dump
on:
schedule:
- cron: "17 3 * * *"
workflow_dispatch:
jobs:
dump:
runs-on: ubuntu-latest
env:
DATABASE_URL: ${{ secrets.REPLICA_DATABASE_URL }}
ANON_SALT: ${{ secrets.ANON_SALT }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
S3_ENDPOINT_URL: ${{ secrets.S3_ENDPOINT_URL }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: { python-version: "3.12" }
- run: git clone --depth 1 https://github.com/fadadyounes-pixel/cloud-db-anonymizer anonymizer
- run: pip install -r anonymizer/requirements.txt
- run: |
python anonymizer/main.py sanitize \
--rules rules.json \
--output staging_$(date -u +%F).sql.gz \
--upload my-sanitized-dumps
The dump contains the schema (via pg_dump --schema-only), data in foreign-key order, sequence values, identity columns, JSONB and arrays. If pg_dump is missing from the runner, the CLI falls back to a data-only export and prints a warning.
One gotcha: pg_dump refuses to dump from a server with a newer major version than itself (see the pg_dump documentation). If your production server is newer than the client on the runner, install the matching postgresql-client package from the PostgreSQL apt repository before the sanitize step.
Step 5: Restore it wherever you need it
gunzip -c staging_2026-10-31.sql.gz | psql "$STAGING_DATABASE_URL"
Integration-test jobs can download the latest dump from the bucket and restore it into their own service container, giving every test run production-shaped data that contains no real customer.
Why not upload it as a workflow artifact?
You can, with actions/upload-artifact, but artifacts are downloadable by anyone with read access to the repository, which for a public repository means everyone. Even sanitized, a dump reveals your schema and data volumes. A private bucket with its own access policy and lifecycle rules is the safer home.
Checklist before you turn it on
- Read-only database role on a replica, stored as a secret.
- Private
ANON_SALT, never the default. - Free-text columns (support tickets, notes) set to
redactortext; secrets set tohash; large blobs set toskip_data. - The pull-request dry run is a required check.
- Restore the first dump yourself and grep it for your own email address.
When a GitHub Actions cron is not enough
The workflow above covers one database and one bucket, which is exactly what the free CLI does. If you want a web dashboard and a rules editor, a self-hosted scheduler, parallel uploads to AWS S3, Cloudflare R2 and DigitalOcean Spaces with retention pruning, and Telegram or Discord alerts when a run fails, that is what Cloud-DB-Anonymizer Pro Suite adds as a Docker Compose stack you run on your own infrastructure. For the full manual walkthrough without CI, read how to anonymize a PostgreSQL production database for staging, or see an overview of the tool on the PostgreSQL PII anonymizer page.