四月 5, 2024
摘要:这部分文档是 pgcopydb 命令的参考手册。
本文包含以下部分:
pgcopydb - 将整个 Postgres 数据库从源端实例复制到目标实例
概要
pgcopydb 提供了以下命令
pgcopydb: pgcopydb tool
usage: pgcopydb [ --verbose --quiet ]
Available commands:
pgcopydb
clone Clone an entire database from source to target
fork Clone an entire database from source to target
follow Replay changes from the source database to the target database
snapshot Create and export a snapshot on the source database
+ compare Compare source and target databases
+ copy Implement the data section of the database copy
+ dump Dump database objects from a Postgres instance
+ restore Restore database objects into a Postgres instance
+ list List database objects from a Postgres instance
+ stream Stream changes from the source database
ping Attempt to connect to the source and target instances
help Print help message
version Print pgcopydb version
描述
pgcopydb 命令实现了,将整个 Postgres 数据库从源端实例完全迁移到目标实例。在命令的整个持续时间内,这两个 Postgres 实例都必须是可用的。
pgcopydb 命令还为 Postgres 实现了一个完整的逻辑解码客户端,在基础副本快照之后,允许更改数据捕获重放源数据库上发生的数据更改(DML)。pgcopydb 逻辑解码客户端代码与 test_decoding 和 wal2json 输出插件兼容,默认使用 test_decoding。
pgcopydb help
pgcopydb help
命令会列出所有支持的子命令:
pgcopydb
clone Clone an entire database from source to target
fork Clone an entire database from source to target
follow Replay changes from the source database to the target database
snapshot Create and export a snapshot on the source database
+ compare Compare source and target databases
+ copy Implement the data section of the database copy
+ dump Dump database objects from a Postgres instance
+ restore Restore database objects into a Postgres instance
+ list List database objects from a Postgres instance
+ stream Stream changes from the source database
ping Attempt to connect to the source and target instances
help Print help message
version Print pgcopydb version
pgcopydb compare
schema Compare source and target schema
data Compare source and target data
pgcopydb copy
db Copy an entire database from source to target
roles Copy the roles from the source instance to the target instance
extensions Copy the extensions from the source instance to the target instance
schema Copy the database schema from source to target
data Copy the data section from source to target
table-data Copy the data from all tables in database from source to target
blobs Copy the blob data from the source database to the target
sequences Copy the current value from all sequences in database from source to target
indexes Create all the indexes found in the source database in the target
constraints Create all the constraints found in the source database in the target
pgcopydb dump
schema Dump source database schema as custom files in work directory
pre-data Dump source database pre-data schema as custom files in work directory
post-data Dump source database post-data schema as custom files in work directory
roles Dump source database roles as custome file in work directory
pgcopydb restore
schema Restore a database schema from custom files to target database
pre-data Restore a database pre-data schema from custom file to target database
post-data Restore a database post-data schema from custom file to target database
roles Restore database roles from SQL file to target database
parse-list Parse pg_restore --list output from custom file
pgcopydb list
databases List databases
extensions List all the source extensions to copy
collations List all the source collations to copy
tables List all the source tables to copy data from
table-parts List a source table copy partitions
sequences List all the source sequences to copy data from
indexes List all the indexes to create again after copying the data
depends List all the dependencies to filter-out
schema List the schema to migrate, formatted in JSON
progress List the progress
pgcopydb stream
setup Setup source and target systems for logical decoding
cleanup Cleanup source and target systems for logical decoding
prefetch Stream JSON changes from the source database and transform them to SQL
catchup Apply prefetched changes from SQL files to the target database
replay Replay changes from the source to the target database, live
+ sentinel Maintain a sentinel table on the source database
receive Stream changes from the source database
transform Transform changes from the source database into SQL commands
apply Apply changes from the source database into the target database
pgcopydb stream sentinel
setup Setup the sentinel table
get Get the sentinel table values on the source database
+ set Maintain a sentinel table on the source database
pgcopydb stream sentinel set
startpos Set the sentinel start position LSN on the source database
endpos Set the sentinel end position LSN on the source database
apply Set the sentinel apply mode on the source database
prefetch Set the sentinel prefetch mode on the source database
pgcopydb version
pgcopydb version
命令可输出使用的 pgcopydb 版本的版本字符串,当使用--json
选项时,可以以 JSON 格式执行此操作。
$ pgcopydb version
pgcopydb version 0.13.1.g868ad77
compiled with PostgreSQL 13.11 (Debian 13.11-0+deb11u1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
compatible with Postgres 10, 11, 12, 13, 14, and 15
以 JSON 格式:
$ pgcopydb version --json
{
"pgcopydb": "0.13.1.g868ad77",
"pg_major": "13",
"pg_version": "13.11 (Debian 13.11-0+deb11u1)",
"pg_version_str": "PostgreSQL 13.11 (Debian 13.11-0+deb11u1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit",
"pg_version_num": 130011
}
关于 Postgres 版本的详细信息,适用于从源代码构建 pgcopydb 的版本,因此这实际上是客户端库libpq
的版本。
pgcopydb ping
pgcopydb ping
命令尝试同时连接到源端和目标 Postgres 数据库。
pgcopydb ping: Attempt to connect to the source and target instances
usage: pgcopydb ping --source ... --target ...
--source Postgres URI to the source database
--target Postgres URI to the target database
样例输出如下所示:
$ pgcopydb ping
18:04:48 84679 INFO Running pgcopydb version 0.10.31.g7e5fbb8.dirty from "/Users/dim/dev/PostgreSQL/pgcopydb/src/bin/pgcopydb/pgcopydb"
18:04:48 84683 INFO Successfully could connect to target database at "postgres://@:/plop?"
18:04:48 84682 INFO Successfully could connect to source database at "postgres://@:/pagila?"
此命令实现了一种重试策略(名为 去相关抖动),可用于自动化,以确保数据库已准备好接受连接。