PostgreSQL数据备份与恢复pg_dump&pg_restore&psql的使用

数据库的备份与恢复是最常用的功能之一,无论哪种数据库都会提供这方面的工具。PostgreSQL提供的对应工具为pg_dump和pg_restore,其中pg_dump用于数据备份,pd_restore用于数据恢复。那psql用来做什么呢?使用pg_dump转储格式可以是一个脚本或者归档文件。如果转储脚本的格式是纯文本(包含许多SQL命令),如果做恢复操作,就需要使用psql命令用于重建该数据库并将之恢复到保存脚本时的状态。

当我们使用一个不会的命令时,当然首先查看帮助信息。

$ 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
  --enable-row-security        enable row security (dump only content user has
                               access to)
  --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
  --snapshot=SNAPSHOT          use given snapshot for the dump
  --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

参数翻译如下:

pg_dump 把一个数据库转储为纯文本文件或者是其它格式.

用法:
  pg_dump [选项]... [数据库名字]

一般选项:
  -f, --file=FILENAME          输出文件或目录名
  -F, --format=c|d|t|p         输出文件格式 (定制, 目录, tar)
                               明文 (默认值))
  -v, --verbose                详细模式
  -V, --version                输出版本信息,然后退出
  -Z, --compress=0-9           被压缩格式的压缩级别
  --lock-wait-timeout=TIMEOUT  在等待表锁超时后操作失败
  -?, --help                   显示此帮助, 然后退出

控制输出内容选项:
  -a, --data-only              只转储数据,不包括模式
  -b, --blobs                  在转储中包括大对象
  -c, --clean                  在重新创建之前,先清除(删除)数据库对象
  -C, --create                 在转储中包括命令,以便创建数据库
  -E, --encoding=ENCODING      转储以ENCODING形式编码的数据
  -n, --schema=SCHEMA          只转储指定名称的模式
  -N, --exclude-schema=SCHEMA  不转储已命名的模式
  -o, --oids                   在转储中包括 OID
  -O, --no-owner               在明文格式中, 忽略恢复对象所属者

  -s, --schema-only            只转储模式, 不包括数据
  -S, --superuser=NAME         在明文格式中使用指定的超级用户名
  -t, --table=TABLE            只转储指定名称的表
  -T, --exclude-table=TABLE    不转储指定名称的表
  -x, --no-privileges          不要转储权限 (grant/revoke)
  --binary-upgrade             只能由升级工具使用
  --column-inserts             以带有列名的INSERT命令形式转储数据
  --disable-dollar-quoting     取消美元 (符号) 引号, 使用 SQL 标准引号
  --disable-triggers           在只恢复数据的过程中禁用触发器
  --exclude-table-data=TABLE   不转储指定名称的表中的数据
  --inserts                    以INSERT命令,而不是COPY命令的形式转储数据
  --no-security-labels         不转储安全标签的分配
  --no-tablespaces             不转储表空间分配信息
  --no-unlogged-table-data     不转储没有日志的表数据
  --quote-all-identifiers      所有标识符加引号,即使不是关键字
  --section=SECTION            备份命名的节 (数据前, 数据, 及 数据后)
  --serializable-deferrable    等到备份可以无异常运行
  --use-set-session-authorization
                               使用 SESSION AUTHORIZATION 命令代替
                ALTER OWNER 命令来设置所有权

联接选项:
  -h, --host=主机名        数据库服务器的主机名或套接字目录
  -p, --port=端口号        数据库服务器的端口号
  -U, --username=名字      以指定的数据库用户联接
  -w, --no-password        永远不提示输入口令
  -W, --password           强制口令提示 (自动)
  --role=ROLENAME          在转储前运行SET ROLE

如果没有提供数据库名字, 那么使用 PGDATABASE 环境变量
的数值.

报告错误至 <pgsql-bugs@postgresql.org>.

使用纯文件格式

导出示例:

  1. 导出postgres数据库(包括数据)
    $ pg_dump -U postgres -f /dbname.sql dbname
    
  2. 导出postgres数据库(不包括数据)
    $ pg_dump -U postgres -f /dbname.sql -s dbname
    

恢复数据:

$ psql -U postgres -f /dbname.sql dbname

使用归档文件格式

pg_dump导出归档文件格式,并使用pg_restore重建数据库。

  1. 导出tar归档文件,然后恢复
    $ pg_dump -U postgres -F t -f /bak01.tar bak01 #备份下来有800多k
    $ pg_restore -U postgres -d bak02 /bak01.tar
    
  2. 导出适用于pg_restore的客户化文档,然后恢复
    $ pg_dump -U postgres -F c -f /bak01.dump bak01 #备份下来有300多k
    $ pg_restore -U postgres -d bak02 /bak01.dump 
    

使用压缩格式

处理大数据库:

  1. 使用压缩的转储. 使用你熟悉的压缩程序,比如说 gzip。
    先备份,然后恢复
    $ pg_dump -U postgres bak01 | gzip > /bak01.gz #备份下来只有30多k
    $ gunzip -c /bak01.gz | psql -U postgres bk02
    

    恢复也可以用

    $ cat /bak01.gz | gunzip | psql -U postgres bk02
    
  2. 使用 split 命令,它允许你用下面的方法把输出分解成操作系统可以接受的大小。比如,让每个块大小为1兆字节:
    先备份,然后恢复
    $ pg_dump -U postgres -d bak01 | split -b 100k - /bak01
    $ cat /bak01* | psql -U postgres bk02
    

    导出结果,类似下面
    bak01a 100k
    bak01b 100k
    bak01c 100k
    bak01d 16k

注意
1、使用pg_restore无法恢复纯文本格式的脚本

$ pg_restore -U postgres -d bak01 bak01.sql 
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

2、处于向下兼容的考虑,pg_dump缺省时并不转储大对象,如果需要转储大对象,-F 参数必须使用 ct,并且需要使用 -b 参数。

备份:pg_dump -h localhost -p 5432 -U user01 -W -F c -b -v -f "/home/user01/db01.backup" db01
恢复:pg_restore -h 192.168.1.100 -p 5432 -U user02 -W -d db02 -v "/root/db01.backup"

参考文章
PostgreSQL pg_dump&psql 数据的备份与恢复

如需转载,请注明出处: https://www.chadou.me/p/187

最新发布