可以使用pg_dump --help 查看
[postgres@pg01 ~]$ pg_dump --help
pg_dump dumps a database as a text file or to other formats.
Usage:
pg_dump [OPTION]... [DBNAME]
General options:
-f, --file=FILENAME output file or directory name
-F, --format=c|d|t|p output file format (custom, directory, tar,
plain text (default))
-j, --jobs=NUM use this many parallel jobs to dump
-v, --verbose verbose mode
-V, --version output version information, then exit
-Z, --compress=0-9 compression level for compressed formats
--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
--no-sync do not wait for changes to be written safely to disk
-?, --help show this help, then exit
Options controlling the output content:
-a, --data-only dump only the data, not the schema
-b, --blobs include large objects in dump
-B, --no-blobs exclude large objects in dump
-c, --clean clean (drop) database objects before recreating
-C, --create include commands to create database in dump
-e, --extension=PATTERN dump the specified extension(s) only
-E, --encoding=ENCODING dump the data in encoding ENCODING
-n, --schema=PATTERN dump the specified schema(s) only
-N, --exclude-schema=PATTERN do NOT dump the specified schema(s)
-O, --no-owner skip restoration of object ownership in
plain-text format
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME superuser user name to use in plain-text format
-t, --table=PATTERN dump the specified table(s) only
-T, --exclude-table=PATTERN do NOT dump the specified table(s)
-x, --no-privileges do not dump privileges (grant/revoke)
--binary-upgrade for use by upgrade utilities only
--column-inserts dump data as INSERT commands with column names
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers disable triggers during data-only restore
--enable-row-security enable row security (dump only content user has
access to)
--exclude-table-data=PATTERN do NOT dump data for the specified table(s)
--extra-float-digits=NUM override default setting for extra_float_digits
--if-exists use IF EXISTS when dropping objects
--include-foreign-data=PATTERN
include data of foreign tables on foreign
servers matching PATTERN
--inserts dump data as INSERT commands, rather than COPY
--load-via-partition-root load partitions via the root table
--no-comments do not dump comments
--no-publications do not dump publications
--no-security-labels do not dump security label assignments
--no-subscriptions do not dump subscriptions
--no-table-access-method do not dump table access methods
--no-tablespaces do not dump tablespace assignments
--no-toast-compression do not dump TOAST compression methods
--no-unlogged-table-data do not dump unlogged table data
--on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands
--quote-all-identifiers quote all identifiers, even if not key words
--rows-per-insert=NROWS number of rows per INSERT; implies --inserts
--section=SECTION dump named section (pre-data, data, or post-data)
--serializable-deferrable wait until the dump can run without anomalies
--snapshot=SNAPSHOT use given snapshot for the dump
--strict-names require table and/or schema include patterns to
match at least one entity each
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership
Connection options:
-d, --dbname=DBNAME database to dump
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
--role=ROLENAME do SET ROLE before dump
If no database name is supplied, then the PGDATABASE environment
variable value is used.
Pg _ dump 是一个用于备份 PostgreSQL 数据库的实用程序。即使数据库正在并发使用,它也会进行一致的备份。Pg _ dump 不会阻止其他用户访问数据库(读取器或写入器)。Pg _ dump 只转储一个数据库。要备份整个集群,或者备份集群中所有数据库共有的全局对象(如角色和表空间) ,请使用 pg _ dumpall (1)。转储可以以脚本或归档文件格式输出。脚本转储是纯文本文件,包含将数据库重建为保存时的状态所需的 SQL 命令。要从这样的脚本还原,请将其提供给 psql (1)。脚本文件甚至可以用于在其他计算机和其他体系结构上重建数据库; 经过一些修改,甚至可以在其他 SQL 数据库产品上重建数据库。为了重新构建数据库,必须将替代的归档文件格式与 pg_restore (1)一起使用。它们允许 pg_restore 选择要还原的内容,甚至允许在还原之前重新排序项。存档文件格式的设计是跨架构可移植的。当与一种归档文件格式一起使用并与 pg_restore 结合使用时,pg _ dump 提供了一种灵活的归档和传输机制。Pg _ dump 可用于备份整个数据库,然后pg_restore 可用于检查归档文件和/或选择要还原数据库的哪些部分。最灵活的输出文件格式是“自定义”格式(- Fc)和“目录”格式(- Fd)。它们允许对所有归档的项目进行选择和重新排序,支持并行恢复,并且在默认情况下进行压缩。“目录”格式是唯一支持并行转储的格式。在运行 pg _ dump 时,应该检查输出中是否有任何警告(打印在标准错误上) ,特别是考虑到下面列出的限制。
pg_dump dumps a database as a text file or to other formats.(pg_dump 将一个数据库备份为一个文件或者其他格式)
Usage:
pg_dump [OPTION]... [DBNAME] (语法使用结构)
General options:
-f, --file=FILENAME output file or directory name(输出文件或路径的名称)
-F, --format=c|d|t|p output file format (custom, directory, tar,
plain text (default)) [输出文件格式(custom, directory, tar,
plain text (default)),在不指定此参数的情况下 默认是plain text 。其实就是一个可执行的SQL文本]
-j, --jobs=NUM use this many parallel jobs to dump(指定并发数)
-v, --verbose verbose mode (详细模式)
-V, --version output version information, then exit (输出详细信息然后退出)
-Z, --compress=0-9 compression level for compressed formats(压缩格式的压缩等级)
--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock(等待表锁超时失败)
--if-exists
Use conditional commands (i.e., add an IF EXISTS clause) when cleaning database objects. This option is not valid unless --clean is also specified.(在清理数据库对象时使用条件命令(例如,添加 IF EXISTS 子句)。此选项无效,除非还指定了—— clean。)
Options controlling the output content:
-a, --data-only dump only the data, not the schema (仅仅备份数据,排除schema)
-b, --blobs include large objects in dump(包括备份大项目)
-B, --no-blobs exclude large objects in dump(备份排除大项目)
-c, --clean clean (drop) database objects before recreating(在重新创建之前清理(删除)数据库对象)
-C, --create include commands to create database in dump(在备份命令中包含创建数据库命令)
-e, --extension=PATTERN dump the specified extension(s) only(只转储指定的扩展名)
-E, --encoding=ENCODING dump the data in encoding ENCODING(以编码方式转储数据)
-n, --schema=PATTERN dump the specified schema(s) only(只转储指定的schema)
-N, --exclude-schema=PATTERN do NOT dump the specified schema(s)(备份时排除指定的schema)
-O, --no-owner skip restoration of object ownership in
plain-text format(以plain文本恢复时跳过关系恢复)
-s, --schema-only dump only the schema, no data(只是备份schema,不备份数据)
-S, --superuser=NAME superuser user name to use in plain-text format(在备份的plain文本中使用超级用户恢复)
-t, --table=PATTERN dump the specified table(s) only(备份指定的表)
-T, --exclude-table=PATTERN do NOT dump the specified table(s)(不备份指定的表)
-x, --no-privileges do not dump privileges (grant/revoke)(不备份权限)
--binary-upgrade for use by upgrade utilities only(仅供升级使用)
Connection options:()
-d, --dbname=DBNAME database to dump(指定备份数据库)
-h, --host=HOSTNAME database server host or socket directory(指定备份数据库IP)
-p, --port=PORT database server port number(指定备份数据库端口号)
-U, --username=NAME connect as specified database user(指定用于备份数据库时,使用备份库的用户)
-w, --no-password never prompt for password(从不提示输入密码)
-W, --password force password prompt (should happen automatically)(强制提示密码(应该自动发生))
--role=ROLENAME do SET ROLE before dump(在转储之前设置角色)
If no database name is supplied, then the PGDATABASE environment
variable value is used.(如果未提供数据库名称,则使用 PGDATABASE 环境变量值。)
测试两个库是否能正常链接
pg01为接收备份服务 下文使用pg01描述
pg02 为远端备份库,下文使用pg02描述
通过pg01服务端可以远程链接到远程pg02端,即两个服务端互通可以进行后续备份操作,若是无法连接请检查端口,监听,输入名称,防火墙IP白名单等
备份pg02到 pg01上并使用plain文本格式储存
pg_dump -U postgres -p 25432 -d postgres -h 10.0.0.199 -Fp -f pg02.sql -v -c
-U 用户名
-p 端口号
-d 数据库名称
-h IP地址
-Fp 存储为plain格式
-f 文件名
-v 列出复制过程详细信息
-c 在输出创建数据库对象的命令之前清理(删除)数据库对象的输出命令。(如果目标数据库中没有任何对象,还原可能会生成一些错误消息,除非还指定了—— if-exists。)在发出归档(非文本)输出文件时忽略此选项。对于归档格式,您可以在调用 pg_restore时指定该选项。
plain备份的plain 文本就是一个可执行的SQL 文件
可以使用指令
cat pg02.sql |head -20
##查看该文件前20行内容
因为备份文件格式为plain文本 ,
所以将pg02备份文件恢复到pg01数据库上时,使用psql -f 即可(备份文本为其他格式需使用pg_restore进行恢复)。
psql -f pg02.sql -d postgres -U pg01 -p 15432
-f 执行文件名
-d 运行数据库名
-U 运行用户
-p 端口号
可以看到两边数据库结构一致
当备份文件不是plain文本格式时 我就需要通过pg_restore 进行恢复覆盖
pg_dump -Fd -f pg02.sql -d postgres -h 10.0.0.199 -p 25432 -U postgres -v -c --if-exists
输出一个目录格式的归档文件,适合输入到 pg_restore. 中。这将为每个被转储的表和 blob 创建一个目录,其中包含一个文件,以及一个所谓的“目录”文件,该文件以机器可读的格式描述被转储的对象, pg_restore.可以读取这些文件。可以使用标准的 Unix 工具操作目录格式的归档文件; 例如,可以使用 gzip 工具压缩未压缩归档文件中的文件。默认情况下,此格式是压缩的,并且还支持并行转储。
将备份文件恢复到pg01服务器的pg01 database中去
pg_restore pg02/ -p 15432 -d pg01 -v
恢复执行成功。
使用命令查看相关参数介绍
pg_restore --help
man pg_restore
pg_restore 从一个归档中恢复一个由 pg_dump 创建的 PostgreSQL 数据库.
用法:
pg_restore [选项]… [文件名]
一般选项:
-d, --dbname=名字 连接数据库名字
-f, --file=文件名 输出文件名
-F, --format=c|d|t backup file format (should be automatic)
-l, --list 打印归档文件的 TOC 概述
-v, --verbose 详细模式
–help 显示此帮助信息, 然后退出
–version 输出版本信息, 然后退出恢复控制选项:
-a, --data-only 只恢复数据, 不包括模式
-c, --clean 在重新创建数据库对象之前需要清除(删除)数据库对象
-C, --create 创建目标数据库
-e, --exit-on-error 发生错误退出, 默认为继续
-I, --index=名称 恢复指定名称的索引
-j, --jobs=NUM 可以执行多个任务并行进行恢复工作
-L, --use-list=文件名 从这个文件中使用指定的内容表排序输出
-n, --schema=NAME 在这个模式中只恢复对象
-O, --no-owner 忽略恢复对象所属者
-P, --function=名字(参数) 恢复指定名字的函数
-s, --schema-only 只恢复模式, 不包括数据
-S, --superuser=NAME 使用指定的超级用户来禁用触发器
-t, --table=NAME 恢复指定命字的表
-T, --trigger=NAME 恢复指定命字的触发器
-x, --no-privileges 跳过处理权限的恢复 (grant/revoke)
-1, --single-transaction 作为单个事务恢复
–disable-triggers 在只恢复数据的过程中禁用触发器
–no-data-for-failed-tables 没有恢复无法创建表的数据
–no-security-labels do not restore security labels
–no-tablespaces 不恢复表空间的分配信息
–use-set-session-authorization 使用 SESSION AUTHORIZATION 命令代替ALTER OWNER命令来设置对象所有权
联接选项:
-h, --host=主机名 数据库服务器的主机名或套接字目录
-p, --port=端口号 数据库服务器的端口号
-U, --username=名字 以指定的数据库用户联接
-w, --no-password 永远不提示输入口令
-W, --password 强制口令提示 (自动)
–role=ROLENAME 在恢复前执行SET ROLE操作
————————————————
版权声明:本文为CSDN博主「ac.char」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sunny_day_day/article/details/106531687
pg_dump 与pg_dumpall最大的区别 当远端备份库在一个实例中有多个database时 可以使用pg_dumpall进行备份。而pg_dump 只能单次备份单个数据库
此时我们在远端库pg02中 有多个database 与pg01的database不同 我们使用pg_duampall对pg02进行备份
pg_dumpall -f dir_pg02 -h 10.0.0.199 -p 25432 -U postgres -v -c --if-exists
pg_dumpall进行备份时 默认备份为可以可读写的文本格式
-f 文件夹名称
-h 远端服务器IP
-p 端口号
-U 运行用户
-v 列出执行过程详细信息
-c --if-exists 在恢复中执行情况目标项目,并植入if eixsts命令
使用psql -f 进行恢复
psql -f dir_pg02 -p 15432
此时完成恢复后 pg01也备份了pg02、postgres两个database
看完记得多多练习相关参数的使用