Skip to main content

Crate pg_dbmigrator

Crate pg_dbmigrator 

Source
Expand description

§pg_dbmigrator

A Rust library for migrating PostgreSQL databases between two endpoints.

  • Offline — performs pg_dump against the source and pg_restore (or psql) against the target. Equivalent to a one-shot dump-and-load.
  • Online — first creates a logical replication slot on the source with EXPORT_SNAPSHOT, runs a snapshot-consistent pg_dump / pg_restore, and then issues CREATE SUBSCRIPTION on the target so PostgreSQL’s built-in apply worker streams WAL changes from the slot until the operator triggers cutover.

The crate is split into small, single-purpose modules so that it can be consumed both as a library and from the bundled CLI binary.

§High level usage

use pg_dbmigrator::{MigrationConfig, MigrationMode, Migrator, EndpointConfig};
use tokio_util::sync::CancellationToken;

let cfg = MigrationConfig {
    mode: MigrationMode::Offline,
    source: EndpointConfig::parse("postgresql://user:pw@src/db")?,
    target: EndpointConfig::parse("postgresql://user:pw@dst/db")?,
    ..MigrationConfig::default()
};

let migrator = Migrator::new(cfg);
migrator.run(CancellationToken::new()).await?;

See the examples/ directory for full programs.

Re-exports§

pub use config::CutoverConfig;
pub use config::EndpointConfig;
pub use config::MigrationConfig;
pub use config::MigrationMode;
pub use config::OnlineOptions;
pub use config::ReplicationApplyConfig;
pub use cutover::CutoverHandle;
pub use cutover::LagSampler;
pub use cutover::Transition;
pub use error::MigrationError;
pub use error::Result;
pub use orchestrator::cleanup_source_after_cutover;
pub use orchestrator::Migrator;
pub use progress::JsonReporter;
pub use progress::MigrationStage;
pub use progress::ProgressEvent;
pub use progress::ProgressReporter;
pub use resume::CompletedStage;
pub use resume::ResumeToken;
pub use resume::RESUME_SCHEMA_VERSION;

Modules§

analyze
Pre-dump source VACUUM ANALYZE and post-restore target ANALYZE.
config
Configuration types used to drive a migration.
cutover
Cutover handle + lag detection helpers for online migrations.
dump
Wrapper around the pg_dump external command.
error
Error types used throughout the migrator.
native_apply
Native PostgreSQL logical-replication apply engine.
orchestrator
High-level migration driver.
preflight
Pre-flight environment checks run before any migration work begins.
progress
Migration progress reporting.
restore
Wrapper around pg_restore (and psql for plain SQL dumps).
resume
Resume token persisted between runs of crate::Migrator::run.
sequences
Sequence sync at cutover (online mode only).
snapshot
Snapshot / replication-slot management for online migrations.
tls
TLS helpers for talking to PostgreSQL.