postgresql使用pg_dump备份数据和恢复数据
一、备份
先上一张官方文档
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))
# -Fd标识directory模式 custom和directory的方式备份比较快
-j, --jobs=NUM use this many parallel jobs to dump
# 根据CPU数据来,并行备份比较快,但是有的版本只支持-Fd模式
-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
# 在删除对象时使用IF EXISTS
--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-synchronized-snapshots do not use synchronized snapshots in parallel jobs
--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 # 指定数据库的host
-p, --port=PORT database server port number # 指定要转储的数据库的port
-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.
Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
1. 所有数据库备份
要备份所有数据库,您可以按顺序对每个数据库运行上面的pg_dump命令,如果您想加快备份过程,也可以并行运行。
首先,在 psql 中,使用命令\list列出实例中所有可用的数据库。
其次,使用上一节中所述的pg_dump程序备份每个单独的数据库 。
除了pg_dump程序之外,PostgreSQL 还为您提供了pg_dumpall工具,它允许您一次备份所有数据库。但不建议使用该工具,原因如下:
pg_dumpall程序将所有数据库依次导出到单个脚本文件中,这会阻止您执行并行恢复。如果您以这种方式备份所有数据库,恢复过程将花费更多时间。
转储所有数据库的处理时间比单个数据库的处理时间长,因此您不知道哪个数据库的转储与特定时间点相关。
如果您有充分的理由使用pg_dumpall来备份所有数据库,可以使用下面的命令:
pg_dumpall -U postgres > c:\pgbackup\all.sql
pg_dumpall程序的选项与pg_dump程序的选项类似。此命令省略了-W选项,避免键入每个单独数据库的密码。
2. 单库库备份
命令:pg_dump -U [username] -W [password] -d [database] -F [c|d|t|p] -f [output path]
以上命令解释:
-U:是用户名。
-W:是用户对应的密码。
-d:指定定了要备份的数据库。
-F: 取值 c|d|t|p 分别对应了自定义存档格式文件,目录格式存档文件,tar压缩包,纯文本sql脚本文件。
-f: 制定了输出的备份文件路径及文件名。
除了以上基本参数外还有几个参数也值得关注
-j 可以指定备份时的并发任务数量,可以大大提高备份速度。
3.单表备份
命令pg_dump -U [username] -W [password] -d [database] -t [tablename] -f [outputfile]
二、恢复
命令:psql -d [database] --echo-errors -f [input file] 2> [log file]
以上命令解释:
--echo-errors:执行恢复时输出错误信息
2>:把错误信息输出到指定文件