Small CLI tool to run PostgreSQL migrations/dumps between two databases. Entrypoint: main.main. Core logic lives in Migrator.Migrate and the dump/restore implementation is in Migrator.DumpDatabase. Relevant files: main.go, Dockerfile, go.mod, pg-migrator, kubernetes/job.yaml, kubernetes/configmap.yaml, kubernetes/secret.yaml.
Prerequisites
- Go (to build locally) or Docker (to run container)
- kubectl + access to a cluster (for Kubernetes)
- If running the built binary locally, ensure
pg_dumpandpg_restoreare available in PATH (the official Docker image includes them).
Quick CLI (local) build and run
- Build the binary:
go build -o pg-migrator main.go- Run the CLI. Required flags are shown in the binary (see
main.main). Example:
./pg-migrator \
-src-host=SRC_HOST -src-port=5432 -src-user=SRC_USER -src-password=SRC_PASSWORD \
-dst-host=DST_HOST -dst-port=5432 -dst-user=DST_USER -dst-password=DST_PASSWORD \
-dump-dir=/tmp/pg_migrationFlags available (defined in main.go):
- -src-host, -src-port, -src-user, -src-password
- -dst-host, -dst-port, -dst-user, -dst-password
- -dump-dir
Note: the binary uses external commands (pg_dump / pg_restore) via exec (see Migrator.DumpDatabase), so they must be present when running locally.
Build Docker image
The repository includes a multi-stage Dockerfile that builds a static Go binary and uses the official Postgres image (so pg_dump/pg_restore are available).
Build:
docker build -t registry.your.domain/pg-migrator/pg-migrator:latest .Dockerfile: Dockerfile
Run container locally (example):
docker run --rm \
-e SRC_HOST=... -e SRC_PORT=5432 -e SRC_USER=... -e SRC_PASSWORD=... \
-e DST_HOST=... -e DST_PORT=5432 -e DST_USER=... -e DST_PASSWORD=... \
registry.your.domain/pg-migrator/pg-migrator:latest \
-dump-dir=/tmp/pg_migrationPush to registry:
docker push registry.your.domain/pg-migrator/pg-migrator:latestKubernetes usage Manifests are provided under kubernetes/. See:
The Job mounts an emptyDir at /tmp/pg_migration and passes flags from environment variables. To deploy:
- Edit
kubernetes/configmap.yamlandkubernetes/secret.yamlfor your environment (or create equivalents). - Update the image tag in kubernetes/job.yaml if you built a custom image.
- Apply manifests:
kubectl apply -f kubernetes/configmap.yaml
kubectl apply -f kubernetes/secret.yaml
kubectl apply -f kubernetes/job.yaml- Watch job and fetch logs:
kubectl get jobs -n default
kubectl logs job/pg-migration-job -n defaultImportant details from the manifests
- The Job disables retries:
backoffLimit: 0and usesttlSecondsAfterFinished: 86400to clean up after 24 hours (see kubernetes/job.yaml). - The Job sets env vars from the ConfigMap and Secret (see kubernetes/job.yaml, kubernetes/configmap.yaml, kubernetes/secret.yaml).
- Dumps are written to an emptyDir with a
10Gisize limit (see kubernetes/job.yaml).
Troubleshooting
- If running locally and you see pg_dump/pg_restore errors, install the Postgres client tools or run the provided Docker image which includes them.
- If the Job cannot pull the image, verify the image name and registry credentials.
- Logs from the binary include success/failure messages from
Migrator.MigrateDatabases(seeMigrator.MigrateDatabases).
References
- Entrypoint:
main.main— main.go - Dump logic:
Migrator.DumpDatabase— main.go - Migration orchestration:
Migrator.Migrate/Migrator.MigrateDatabases— main.go - Dockerfile: Dockerfile
- Go module: go.mod
- Kubernetes manifests: kubernetes/job.yaml, kubernetes/configmap.yaml, kubernetes/secret.yaml
- Prebuilt binary in repo: pg-migrator