pg_dump导出表结构和数据

mpp数据库的master节点,切换到管理员用户执行此命令

帮助文件

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
  --gp-syntax                  dump with Greenplum Database syntax (default if gpdb)
  --no-gp-syntax               dump without Greenplum Database syntax (default if postgresql)
  --function-oids              dump only function(s) of given list of oids
  --relation-oids              dump only relation(s) of given list of oids

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 <bugs@greenplum.org>.

1. 导出结构

1.1. 导出单张表结构

pg_dump -t test -s -f schema.sql your_database_name
  • 将test替换为要导出表结构的表名。
  • 将schema.sql替换为要生成的SQL文件的名称和路径。
  • 将your_database_name替换为要导出表结构的数据库名称。

1.2. 导出以tab_开头的表的结构

pg_dump -t 'tab_*' -s -f schema.sql your_database_name

这个命令中的通配符*用于匹配以"tab_"开头的所有表名。将schema.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出表结构的数据库名称。

请注意,在某些操作系统中,通配符可能需要使用引号括起来,以避免被终端解释。因此,我们在命令中使用了'tab_*'来确保通配符被正确解释。

1.3. 导出test_schema下tab_开头的表的结构

pg_dump -n test_schema -t 'tab_*' -s -f schema.sql your_database_name

这个命令中,-n选项用于指定模式(schema)名称,将test_schema替换为您要导出的模式名称。-t选项用于指定表名的模式,使用通配符tab_*匹配以"tab_"开头的表名。

将schema.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出表结构的数据库名称。

1.4. 导出test_schema下所有资源的结构,包含表,函数,序列等

pg_dump -n test_schema -s -f schema.sql your_database_name

这个命令中,-n选项用于指定模式(schema)名称,将test_schema替换为您要导出的模式名称。

-s选项用于导出模式的结构,包括表、函数、序列等。

将schema.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出结构的数据库名称。

2. 导出数据

2.1. 导出test_schema下test_001表里的数据

pg_dump -t test_schema.test_001 -a -f data.sql your_database_name

这个命令中,-t选项用于指定要导出的表,将test_schema.test_001替换为您要导出数据的表的完整名称。

-a选项用于导出表的数据。

将data.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出数据的数据库名称。

2.2. 导出test_schema下test_开头的表的数据

pg_dump -t 'test_schema.test_*' -a -f data.sql your_database_name

这个命令中,-t选项用于指定要导出的表,将test_schema.test_*替换为您要导出数据的表的模式和通配符。

-a选项用于导出表的数据。

将data.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出数据的数据库名称

2.3. 导出名为test_schema的schema下的所有数据到文件

pg_dump -n test_schema -a -f data.sql your_database_name

这个命令中,-n选项用于指定要导出的模式,将test_schema替换为您要导出数据的模式名称。

-a选项用于导出模式下所有表的数据。

将data.sql替换为要生成的SQL文件的名称和路径,将your_database_name替换为要导出数据的数据库名称。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值