pg_dump 详解/使用举例

简介: pg_dump是一个用于备份PostgreSQL数据库的实用工具。即使当前数据库正在使用,也能够生成一致性的备份,且不会阻塞其他用户访问数据库(包括读、写)pg_dump只能备份一个数据库。如果要备份Cluster中数据库共有的全局对象,例如角色和表空间,需要使用pg_dumpall。

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

相关文章
|
人工智能 达摩院 自然语言处理
超好用的开源模型平台,ModelScope阿里达摩院
超好用的开源模型平台,ModelScope阿里达摩院
2091 1
|
Oracle 关系型数据库 数据库
postgresql|数据库|数据迁移神器ora2pg的安装部署和初步使用
postgresql|数据库|数据迁移神器ora2pg的安装部署和初步使用
1935 0
|
10月前
|
人工智能 分布式计算 DataWorks
阿里云大数据AI产品月刊-2025年12月
大数据& AI 产品技术月刊【2025年 12 月】,涵盖 12 月技术速递、产品和功能发布、市场和客户应用实践等内容,帮助您快速了解阿里云大数据& AI 方面最新动态。
|
人工智能 自然语言处理 监控
阿里云ModelScope平台的综合测评
ModelScope是阿里云推出的AI模型全生命周期管理平台,涵盖模型开发、训练、部署及评测全流程。其核心功能包括预训练模型库、一键式训练与部署、模型版本管理等。中文竞技场作为子平台,专注于模型性能对比与多场景任务评测,在写作创作、代码开发支持和知识问答等方面表现出色。然而,平台在模型泛化能力、高并发性能和内容质量控制上仍有改进空间。总体而言,ModelScope为开发者提供了高效便捷的工具,未来有望进一步推动AI技术普惠化。
2048 10
|
存储 算法 安全
JAVA 八股文全网最详尽整理包含各类核心考点助你高效学习 jAVA 八股文赶紧收藏
本文整理了Java核心技术内容,涵盖Java基础、多线程、JVM、集合框架等八股文知识点,包含面向对象特性、线程创建与通信、运行时数据区、垃圾回收算法及常用集合类对比,附有代码示例与学习资料下载链接,适合Java开发者系统学习与面试准备。
2037 0
|
Ubuntu Unix Linux
详细指导:Ubuntu镜像下载及系统安装全过程
安装Ubuntu系统并配置引导文件后,您已经成功地将Ubuntu添加到了您的计算机的多重启动选项中。至此,整个安装与配置过程已经全部完成。
|
存储 固态存储 关系型数据库
PostgreSQL核心操作之数据备份恢复
PostgreSQL核心操作之数据备份恢复
2263 0
Flutter 中使用 ICON
Flutter 中使用 ICON
911 5
Flutter 中使用 ICON
|
缓存 安全 搜索推荐
Cors跨域(三):Access-Control-Allow-Origin多域名?
Cors跨域(三):Access-Control-Allow-Origin多域名?
Cors跨域(三):Access-Control-Allow-Origin多域名?