pg_dump 详解/使用举例

pg_dump是一个用于备份PostgreSQL数据库的实用工具。即使当前数据库正在使用,也能够生成一致性的备份,且不会阻塞其他用户访问数据库(包括读、写)

pg_dump只能备份一个数据库。如果要备份Cluster中数据库共有的全局对象,例如角色和表空间,需要使用pg_dumpall。

备份文件以文本或存档文件格式输出。Script dumps是一个普通文本文件,包含将数据库重构到保存时的状态所需的SQL命令。

要从这样的脚本恢复,需要将其提供给psql。脚本文件甚至可以用来在其他机器或者其他架构上重构数据库;进行一些必要的修改,甚至可以在其他数据库上使用。

其他归档文件格式必须与pg_restore一起使用进行数据库的重建。pg_restore可以选择要还原的内容,甚至可以在还原之前对待还原项进行重新排序。归档文件格式可以在不同的架构中使用。

归档文件格式与pg_restore组合使用时,pg_dump提供了一个灵活的归档传递机制。可以使用pg_dump备份整个数据库。pg_restore可用于检查存档和选择要还原数据库的哪些部分。
最灵活的输出文件格式是“自定义”格式(-Fc)和“目录”格式(-Fd)。它们允许选择和重新排序所有存档项,支持并行恢复,以及默认情况下是压缩的。“目录”格式是唯一支持并行备份的格式。

To dump a database called mydb into a SQL-script file:
$ pg_dump mydb > db.sql

To reload such a script into a (freshly created) database named newdb:
$ psql -d newdb -f test.sql

To dump a database into a custom-format archive file:
$ pg_dump -Fc test > test.dump

To dump a database into a directory-format archive:
$ pg_dump -Fd test -f dumpdir

To dump a database into a directory-format archive in parallel with 5 worker jobs:
$ pg_dump -Fd test -j 5 -f dumpdir5

To reload an archive file into a (freshly created) database named newdb:
$ pg_restore -d dump_test test.dump

To dump a single table named mytab:
$ pg_dump -t mytab mydb > db.sql

To dump all tables whose names start with emp in the detroit schema, except for the table named
employee_log:
$ pg_dump -t 'journal*' -T journal_10 test > test_journal.sql

To dump all schemas whose names start with east or west and end in gsm, excluding any schemas
whose names contain the word test:
$ pg_dump -n 'eastgsm' -n 'westgsm' -N 'test' mydb > db.sql

The same, using regular expression notation to consolidate the switches:
$ pg_dump -n '(east|west)gsm' -N 'test*' mydb > db.sql

To dump all database objects except for tables whose names begin with ts_:
$ pg_dump -T 'ts_*' mydb > db.sql

To specify an upper-case or mixed-case name in -t and related switches, you need to double-quote
the name; else it will be folded to lower case (see Patterns). But double quotes are special to the
shell, so in turn they must be quoted. Thus, to dump a single table with a mixed-case name, you need something like
$ pg_dump -t ""MixedCaseName"" mydb > mytab.sql

上一篇:每天自动发英文外链 247backlinks


下一篇:oracle中dblink创建的两种方式