当前位置:首页 > PostgreSql备份和恢复
PostgreSql备份和恢复
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 -?, --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
-c, --clean clean (drop) database objects before recreating -C, --create include commands to create database in dump -E, --encoding=ENCODING dump the data in encoding ENCODING -n, --schema=SCHEMA dump the named schema(s) only -N, --exclude-schema=SCHEMA do NOT dump the named schema(s) -o, --oids include OIDs in dump
-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=TABLE dump the named table(s) only -T, --exclude-table=TABLE do NOT dump the named 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 --exclude-table-data=TABLE do NOT dump data for the named table(s) --if-exists use IF EXISTS when dropping objects
--inserts dump data as INSERT commands, rather than COPY --no-security-labels do not dump security label assignments
--no-synchronized-snapshots do not use synchronized snapshots in parallel jobs --no-tablespaces do not dump tablespace assignments --no-unlogged-table-data do not dump unlogged table data
--quote-all-identifiers quote all identifiers, even if not key words
--section=SECTION dump named section (pre-data, data, or post-data) --serializable-deferrable wait until the dump can run without anomalies --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.
Report bugs to
博客分类:
?
PostgreSql PostgreSql备份
有三种不同的备份方法:
? ? ?
SQL dump
文件系统级备份(File system level backup) 连续归档(Continuous archiving)
每种都有优势和劣势。
一、SQL Dump
dump方法是生成含有SQL命令的文本文件,当反馈回服务器时,将顺序执行dump中的命令。postgreSql使用pg_dump工具,基础用例是:
Shell代码 1. pg_dump dbname > outfile 这个命令可以在任意可以连接数据库的远程机器上运行,但他需要读表的权限,所以大多数是用superuser用户运行这个命令。
连接指定的数据库可以使用-h host和-p port命令选项。默认的host是local host或由PGHOST环境变量指定。 使用-U选项设置连接数据库的用户。
pg_dump的输出文件可以被更高版本的PostgreSql读取,它也是唯一可以在不同系统间(比如:32位->64位)转移数据的方法。 pg_dump不阻塞数据库的运行。 1、恢复
pg_dump生成的文件由psql读取,一般命令是:
Shell代码 1. psql dbname < infile 当infile是由pg_dump命令生成的,dbname不会被命令创建,所以在执行psql前需要手动创建表。(如:createdb -T template0 dbname) 在执行恢复前,有适当权限的用户必须存在。
默认情况下,psql遇到SQL错误会继续执行。你可以使用ON_ERROR_STOP变量使psql遇到SQL错误时退出,退出状态码是3。
Shell代码 1. psql --set ON_ERROR_STOP=on dbname < infile 如果你想要么全执行,要么全会滚,可以使用-1或--single-transaction选项。
也可以直接从一个服务器备份到另一个服务器:
Shell代码 1. pg_dump -h host1 dbname | psql -h host2 dbname 重要提示:pg_dump生成的转储文件是相对template0中。这意味着,任何语言,程序等通过template1的加入也将经由pg_dump转储。因此,还原时,如果您使用的是客户化的template1,必须从template0中创建空数据库,如上面的例子。
在还原备份后,明智的做法是在每个数据库上运行ANALYZE,以便查询优化器可以使用有效的统计数据。
2、使用pg_dumpall
pg_dump一次转储一个数据库,并且不转储用户角色和表空间。为了方便的转储数据库集群的全部内容,可以使用pg_dumpall
Shell代码 1. pg_dumpall > outfile 恢复是:
Shell代码 1. psql -f infile postgres 恢复pg_dumpall的转储文件需要superuser用户,另外也要确认表空间的路径在新数据库中可用。
pg_dumpall使用emitting命令重建角色、表空间和空数据库,然后对每个数据库使用pg_dump。这意味着虽然每个数据库内会保持一致,但不同数据库的快照可能不完全同步。
3、处理大数据库
一些操作系统有文件大小限制。有几种方法可以处理: 使用压缩
Shell代码 1. pg_dump dbname | gzip > filename.gz 恢复:
Shell代码 1. gunzip -c filename.gz | psql dbname 或者:
Shell代码 1. cat filename.gz | gunzip | psql dbname 使用拆分。可以设置拆分的文件大小:
Shell代码 1. pg_dump dbname | split -b 1m - filename 恢复:
Shell代码 1. cat filename* | psql dbname 使用客户化的转储格式。如果系统有zlib压缩库,输出的客户化格式可以被压缩,压缩文件比gzip更小,它有一个优点,就是可以选择性的恢复某表。
Shell代码 1. pg_dump -Fc dbname > filename 客户化的转储文件不能使用psql恢复,要使用pg_restore:
Shell代码 1. pg_restore -d dbname filename 使用pg_dump的并行转储。要加快大数据库的转储,可以使用pg_dump的并行模式,这可以同时转储多个表。你可以通过-j参数控制并行深度。并行转储只支持目录存储格式。
Shell代码 1. pg_dump -j num -F d -f out.dir dbname 你可以使用pg_restore -j来并行恢复数据。 二、文件系统级别备份
共分享92篇相关文档