Skip to content

A quick tool to migrate from one Postgres version and instance to another. Allowing for Postgresql upgrades in a k8s cluster.

Notifications You must be signed in to change notification settings

Harnish/pg-migrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg-migrator

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_dump and pg_restore are available in PATH (the official Docker image includes them).

Quick CLI (local) build and run

  1. Build the binary:
go build -o pg-migrator main.go
  1. 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_migration

Flags 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_migration

Push to registry:

docker push registry.your.domain/pg-migrator/pg-migrator:latest

Kubernetes usage Manifests are provided under kubernetes/. See:

The Job mounts an emptyDir at /tmp/pg_migration and passes flags from environment variables. To deploy:

  1. Edit kubernetes/configmap.yaml and kubernetes/secret.yaml for your environment (or create equivalents).
  2. Update the image tag in kubernetes/job.yaml if you built a custom image.
  3. Apply manifests:
kubectl apply -f kubernetes/configmap.yaml
kubectl apply -f kubernetes/secret.yaml
kubectl apply -f kubernetes/job.yaml
  1. Watch job and fetch logs:
kubectl get jobs -n default
kubectl logs job/pg-migration-job -n default

Important details from the manifests

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 (see Migrator.MigrateDatabases).

References

About

A quick tool to migrate from one Postgres version and instance to another. Allowing for Postgresql upgrades in a k8s cluster.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages