Repository navigation
Getting Started
- .NET 10 SDK
- PostgreSQL client tools:
pg_dumpandpg_restoreinstalled and onPATH - Access to source and target PostgreSQL databases
Clone the repository and build:
git clone <repo-url>
cd database-migrator-and-sanitizer
dotnet build src/DbMigratorCopy the example config and fill in your database details:
cp config.example.yaml config.yamlMinimum required fields:
migration:
source:
host: "source-db.example.com"
port: 5432
database: "production"
user: "postgres"
password: "${SOURCE_DB_PASSWORD}"
target:
host: "target-db.example.com"
port: 5432
database: "staging"
user: "postgres"
password: "${TARGET_DB_PASSWORD}"
sanitize: []Passwords can be injected via environment variables using ${VAR_NAME} syntax.
db-migrator runs as a dedicated database user. Before the first migration, grant the minimum required privileges on both databases (connect as a superuser, e.g. postgres).
GRANT CONNECT ON DATABASE "your-source-db" TO "db-migrator";
GRANT pg_read_all_data TO "db-migrator";pg_read_all_data (PostgreSQL 14+) covers SELECT on all tables, views, sequences and foreign tables — present and future. It is the only grant needed for pg_dump to read every object in the schema.
GRANT CONNECT ON DATABASE "your-target-db" TO "db-migrator";
GRANT pg_write_all_data TO "db-migrator";
GRANT CREATE ON DATABASE "your-target-db" TO "db-migrator";
GRANT ALL ON SCHEMA public TO "db-migrator";CREATE ON DATABASE and ALL ON SCHEMA public are required because pg_restore --clean drops and recreates tables, indexes, sequences and constraints from scratch.
Run these grants as a superuser via
psqlor your cloud provider's console (e.g. Cloud SQL Studio / RDS Query Editor).
# Validate config and schema against source DB
SOURCE_DB_PASSWORD=... TARGET_DB_PASSWORD=... \
dotnet run --project src/DbMigrator -- validate --config config.yaml
# Preview the migration and sanitization SQL (no changes made)
SOURCE_DB_PASSWORD=... TARGET_DB_PASSWORD=... \
dotnet run --project src/DbMigrator -- run --config config.yaml --dry-run
# Run the full pipeline
SOURCE_DB_PASSWORD=... TARGET_DB_PASSWORD=... \
dotnet run --project src/DbMigrator -- run --config config.yamlOr publish a self-contained binary and run it directly:
dotnet publish src/DbMigrator -c Release -o out/
./out/DbMigrator run --config config.yaml- Write
config.yamlwith sanitization rules - Run
validate— fix any schema mismatches - Run
run --preflight-only— confirm connectivity and DB privileges - Run
run --dry-run— review the generated SQL - Run
run— execute
See CLI Commands for all options and Configuration for the full config reference.