PostgreSQL自带的命令行工具07- pg_dump

PostgreSQL自带的命令行工具07- pg_dump

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

pg_dump 是 PostgreSQL 中用于备份数据库的一个命令行工具。它生成的是一个 SQL 脚本或其他档案文件格式,这个脚本包含了重建数据库所需的数据定义语言 (DDL) 语句(如创建表、索引等),以及插入数据所需的数据操纵语言 (DML) 语句(如插入语句)。pg_dump 对于执行定期备份和迁移数据库至不同服务器非常有用。

通过help查看帮助文档。

[pg16@test ~]$ 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=METHOD[:DETAIL]
                               compress as specified
  --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, --large-objects          include large objects in dump
  --blobs                      (same as --large-objects, deprecated)
  -B, --no-large-objects       exclude large objects in dump
  --no-blobs                   (same as --no-large-objects, deprecated)
  -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 only the specified table(s)
  -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-and-children=PATTERN
                               do NOT dump the specified table(s), including
                               child and partition tables
  --exclude-table-data=PATTERN do NOT dump data for the specified table(s)
  --exclude-table-data-and-children=PATTERN
                               do NOT dump data for the specified table(s),
                               including child and partition tables
  --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
  --table-and-children=PATTERN dump only the specified table(s), including
                               child and partition tables
  --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 <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

基本用法

pg_dump [选项] 数据库名

示例1

备份整个数据库white到一个文件

[pg16@test backup]$ pg_dump white > /home/pg16/backup/white_20240504.sql
[pg16@test backup]$ ll
total 4
-rw-rw-r-- 1 pg16 pg16 1232 May  4 01:47 white_20240504.sql
这个命令将 `white` 数据库的内容导出到 `white_20240504.sql` 文件中。

示例2

备份数据库white下的yewu1.t4 表到一个文件。

[pg16@test backup]$ pg_dump -t 'yewu1.t4' white > /home/pg16/backup/yewu1.t4.sql
[pg16@test backup]$ ll
total 8
-rw-rw-r-- 1 pg16 pg16  825 May  4 01:58 yewu1.t4.sql
使用 `-t` 或 `--table` 选项可以限制输出仅包含指定的表。

示例3

以自定义格式备份并压缩

[pg16@test backup]$ pg_dump -Fc white > /home/pg16/backup/white_20240504.dmp
[pg16@test backup]$ ll
total 16
-rw-rw-r-- 1 pg16 pg16 5227 May  4 01:59 white_20240504.dmp
使用 `-Fc` 选项创建一个自定义格式的备份文件。这种格式较小,可以通过 `pg_restore` 命令进行更灵活的还原。

主要选项

  • -F, --format=c|d|t|p:选择输出格式。c = 自定义,d = 目录,t = tar,p = 纯文本(默认)。
  • -f, --file=文件名:将输出写入指定文件,而不是标准输出。
  • -j, --jobs=并发作业数:使用指定数量的并发作业进行并行导出。这仅适用于某些格式。
  • -t, --table=表名:仅导出指定的表。
  • -v, --verbose:详细模式。
  • -Z, --compress=压缩等级:为自定义格式输出选择压缩等级(0到9)。
  • -C, --create:在输出中包括 SQL 命令以创建数据库本身。
  • -d, --inserts:使用 INSERT 命令代替 COPY 来插入数据。
  • -n, --schema=模式名:仅导出指定的模式。
  • -O, --no-owner:跳过输出文件中的所有者(即不设置所有者)。

注意事项

  • pg_dump 对数据库执行一致性备份,即使在备份进行时数据库仍然在接受写操作。
  • 为了还原 pg_dump 输出的备份,可以使用 psqlpg_restore,这取决于备份的格式。
  • 对于较大的数据库,考虑使用自定义格式或目录格式,并利用并行备份功能来加快速度。
  • 需要适当的数据库访问权限来执行备份。

pg_dump 是数据库维护和管理的重要工具之一,掌握其使用对于保证数据的安全和灵活迁移至关重要。

谨记:心存敬畏,行有所止。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值