pg_dump使用方法[待续]

本文提供了使用pg_dump命令导出PostgreSQL数据库的详细指南,包括导出DDL、数据以及特定模式或表的方法。教程涵盖了pg_dump的常用选项,帮助开发者高效地管理数据库迁移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pg_dump的帮助信息:

[gpadmin@mdw ~]$ 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 name
  -F, --format=c|t|p       output file format (custom, tar, plain text)
  -i, --ignore-version     proceed even when server version mismatches
                           pg_dump version
  -v, --verbose            verbose mode
  -Z, --compress=0-9       compression level for compressed formats
  --help                   show this help, then exit
  --version                output version information, 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) schema prior to create
  -C, --create                include commands to create database in dump
  -d, --inserts            dump data as INSERT, rather than COPY, commands
  -D, --column-inserts     dump data as INSERT commands with column names
  -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        specify the superuser user name to use in
                              plain text format
  -t, --table=TABLE           dump only matching table(s) (or views or sequences)
  -T, --exclude-table=TABLE   do NOT dump matching table(s) (or views or sequences)
  -x, --no-privileges         do not dump privileges (grant/revoke)
  --disable-dollar-quoting    disable dollar quoting, use SQL standard quoting
  --disable-triggers          disable triggers during data-only restore
  --use-set-session-authorization
                              use 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)

Connection options:
  -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, --password           force password prompt (should happen automatically)

If no database name is supplied, then the PGDATABASE environment
variable value is used.

Report bugs to <pgsql-bugs@postgresql.org>.
[gpadmin@mdw ~]$





1、按模式名导出DDL:

pg_dump -h 10.243.32.141 -p 5432 -Fp -i -s -n  dwweb -f dwweb.sql bass_gp

导出的DDL的对象包含表、函数等等

参数说明:

-Fp --format=c|t|p       output file format (custom, tar, plain text)

-i    --ignore-version     proceed even when server version mismatches pg_dump version

-s, --schema-only           dump only the schema, no data

-n, --schema=SCHEMA         dump the named schema(s) only


2、导出一张表的DDL:

pg_dump -h 10.243.32.141 -p 5432 -Fp -i -s -t dwweb.td_b_wlan_product -f dwweb.td_b_wlan_product.sql bass_gp

只导出表的DDL,不包括其他对象


3、导出一张表的DDL和数据:

pg_dump -h 10.243.32.141 -p 5432 -Fp -i -t dwweb.td_b_wlan_product -f dwweb.td_b_wlan_product.sql bass_gp


4、只导出一张表的数据:

pg_dump -h 10.243.32.141 -p 5432 -Fp -i -t dwweb.td_b_wlan_product -a -f dwweb.td_b_wlan_product.sql bass_gp

参数说明:

  -a, --data-only             dump only the data, not the schema







转载于:https://my.oschina.net/goopand/blog/384644

<think>好的,我现在需要解决用户遇到的pg_dump版本与PostgreSQL服务器版本不匹配的问题。用户提到pg_dump是9.2.24,而服务器是9.4.24。首先,我应该回忆一下pg_dump的工作原理以及版本兼容性的相关知识。 根据之前学过的知识,pg_dump工具通常需要与PostgreSQL服务器的版本一致,或者至少是较新的版本才能正确备份较旧的数据库。反过来,旧版本的pg_dump可能无法正确处理新版本服务器的数据结构和功能,导致兼容性问题。用户的情况是pg_dump版本(9.2.24)比服务器版本(9.4.24)旧,这显然不符合要求,因为9.2的pg_dump不支持9.4的服务器。 接下来,我需要找出常见的解决方案。通常,解决方法包括升级pg_dump到与服务器相同的版本,或者使用服务器自带的pg_dump工具。例如,如果服务器是9.4版本,那么安装对应版本的客户端工具,并将pg_dump的路径正确配置到系统PATH中,确保调用的是正确版本。 用户提供的引用中有一个例子是通过创建符号链接来解决版本不一致的问题,比如将高版本的pg_dump链接到系统路径下,覆盖旧版本。例如,引用[3]中提到使用ln -s命令将/usr/pgsql-9.5/bin/pg_dump链接到/usr/bin/pg_dump,并加上--force参数强制替换。这可能是解决用户问题的一个有效方法。 但用户的情况是服务器版本是9.4.24,而pg_dump是9.2.24。因此,正确的做法应该是安装PostgreSQL 9.4版本的客户端工具,确保pg_dump的版本至少是9.4,或者更高。如果系统上已经安装了9.4的客户端工具,可能需要检查PATH环境变量,确保系统优先找到新版本的pg_dump而不是旧版本。 此外,用户可能没有正确安装对应版本的客户端工具。例如,在基于Red Hat的系统上,PostgreSQL的不同版本可能安装在/usr/pgsql-X.Y目录下,如/usr/pgsql-9.4。用户需要确认是否已经安装了9.4的客户端,如果没有,可能需要通过yum或apt安装对应的软件包。 另一个注意事项是,如果用户同时安装了多个PostgreSQL版本,需要注意环境变量的设置,避免不同版本之间的冲突。例如,在命令行中直接调用绝对路径的pg_dump,如/usr/pgsql-9.4/bin/pg_dump,可以确保使用正确的版本。 此外,用户可能遇到的另一个问题是权限问题,在创建符号链接或安装新版本时可能需要root权限,需要使用sudo命令。例如,创建符号链接时需要写权限到/usr/bin目录,这通常需要sudo。 总结解决方案步骤: 1. 确认服务器版本为9.4.24,需要对应的pg_dump版本至少为9.4。 2. 检查系统是否已安装PostgreSQL 9.4的客户端工具,如果没有,安装它们。 3. 将新版本的pg_dump路径添加到系统PATH中,或者创建符号链接覆盖旧版本。 4. 验证pg_dump版本是否正确,执行pg_dump --version查看。 5. 如果环境变量有问题,考虑使用绝对路径调用正确版本的pg_dump。 需要提醒用户注意,直接替换符号链接可能存在风险,尤其是在有其他应用程序依赖旧版本的情况下。因此,最好的做法是安装正确版本的客户端,并调整PATH变量,而不是覆盖系统默认的二进制文件。 此外,用户可能需要更新他们的备份脚本或命令行指令,明确指定pg_dump的路径,避免依赖系统默认路径中的旧版本。 最后,用户的问题可能与引用[2]和引用[3]中的情况类似,都是版本不一致导致的,解决方法也类似,即确保pg_dump版本与服务器一致或更高。</think>要解决$pg_dump$ 9.2.24与PostgreSQL服务器9.4.24版本不匹配的问题,需确保两者版本兼容。以下是具体步骤: ### 解决方案 1. **安装对应版本的客户端工具** 在服务器上安装PostgreSQL 9.4版本的客户端工具包,例如在CentOS/RHEL系统中执行: ```bash sudo yum install postgresql94 ``` 安装后,$pg_dump$的路径通常为`/usr/pgsql-9.4/bin/pg_dump`[^3]。 2. **更新符号链接或PATH环境变量** 将新版本的$pg_dump$设为系统默认路径: ```bash sudo ln -sf /usr/pgsql-9.4/bin/pg_dump /usr/bin/pg_dump ``` 或临时指定路径: ```bash export PATH=/usr/pgsql-9.4/bin:$PATH ``` 3. **验证版本** 执行命令检查版本是否匹配: ```bash pg_dump --version # 应显示9.4.24 ``` 4. **直接使用绝对路径调用** 在备份命令中显式指定路径: ```bash /usr/pgsql-9.4/bin/pg_dump -h 127.0.0.1 -U postgres ... ``` ### 原理说明 - $pg_dump$需与服务器主版本号一致或更高(例如9.4支持9.4.x,但9.2无法兼容9.4)[^1][^2]。 - 旧版工具可能缺少新版数据库的语法解析能力,导致备份失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值