postgres数据库进行备份和恢复

2023-10-27

参考:http://blog.chinaunix.net/uid-354915-id-3504632.html

[root@localhost postgres-9.3.5]# pg_dump --help
pg_dump dumps a database as a text file or to other formats.

Usage:
pg_dump [OPTION]… [DBNAME] 数据库名放最后,不指定默认是系统变量PGDATABASE指定的数据库。

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转储以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在转储中包括 OID
  -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)不要转储权限 (grant/revoke)
  --binary-upgrade             for use by upgrade utilities only只能由升级工具使用
  --column-inserts             dump data as INSERT commands with column names以带有列名的INSERT命令形式转储数据
  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting取消美元 (符号) 引号, 使用 SQL 标准引号
  --disable-triggers           disable triggers during data-only restore在只恢复数据的过程中禁用触发器
  --exclude-table-data=TABLE   do NOT dump data for the named table(s)以INSERT命令,而不是COPY命令的形式转储数据
  --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
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如果没有提供数据库名字, 那么使用 PGDATABASE 环境变量的数值.
variable value is used.

一: 纯文件格式的脚本:

示例:
1. 只导出postgres数据库的数据,不包括模式 -s

   pg_dump -U postgres -f /postgres.sql -s postgres(数据库名)
  1. 导出postgres数据库(包括数据)
   pg_dump -U postgres -f /postgres.sql  postgres(数据库名)
  1. 导出postgres数据库中表test01的数据
   create database "test01" with owner="postgres" encoding='utf-8';(单引号,双引号不能错)
   pg_dump -U postgres -f /postgres.sql -t test01 postgres(数据库名)
  1. 导出postgres数据库中表test01的数据,以insert语句的形式
   pg_dump -U postgres -f /postgres.sql -t test01 --column-inserts postgres(数据库名)
  1. 恢复数据到bk01数据库
  psql -U postgres -f /postgres.sql bk01

二、 使用归档文件格式:

pg_restore
使用pg_restore纯文本恢复纯文本格式的脚本,无法恢复

[root@localhost postgres-9.3.5]# pg_restore -U postgres -d bk01  /mnt/hgfs/window\&ubuntu\ shared\ folder/vendemo.sql 
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

pg_restore和归档文件格式一起使用重建数据库。

  1. 先备份:
   pg_dump -U postgres -F t -f /vendemo.tar vendemo  备份下来有800多k

. 恢复:

   pg_restore -U postgres -d bk01 /vendemo.tar 
  1. 备份:
   pg_dump -U postgres -F c -f /vendemo.tar vendemo  备份下来有300多k

. 恢复:

   pg_restore -U postgres -d bk01 /vendemo.tar 

三、 压缩备份与恢复:

处理大数据库:
1. 使用压缩的转储. 使用你熟悉的压缩程序,比如说 gzip。
. 先备份:

   pg_dump -U postgres vendemo | gzip > /vendemo.gz 备份下来只有30多k

. 恢复:

   gunzip -c /vendemo.gz | psql -U postgres bk02

或者

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

导出来的样子是

   vendaa 100k
   vendab 100k
   vendac 100k
   vendad 16k

. 恢复:

  cat /vend/vend* | psql -U postgres bk02
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

postgres数据库进行备份和恢复 的相关文章

随机推荐

  • 西门子编程基础学习分享(3)-数据类型详述

    1200PLC的数据类型详述 前文所提到的数据类型用于描述数据的长度以及属性 即为指定数据元素的大小以及如何解释数据 每个指令至少支持一种数据类型 因而指令上使用的操作数的数据类型必须与指令所支持的数据类型一致 所以在设计程序 建立变量时需
  • Uva 540 Team Queue

    有t个团体的人正在排一个长队 每次新来一个人时 如果这个成员所在的团体已经有人在排队了 那么他就加到最后一个队友身后 如果整个大队列中没有他的团体 那么他就要排在整个大队列的最后 输入每个团队的人数 每个人的编号 要求支持下面的操作 前两种
  • 【订单服务】库存解锁和关单

    消息队列流程图 监听库存解锁 下单成功 库存锁定成功 接下来的业务调用失败 导致订单回滚 之前锁定的库存就要自动解锁 配置队列和交换机 Configuration public class MyRabbitConfig 使用json序列化机
  • 失业在家靠做PPT日赚800-1000元,有一门副业真的很重要!

    下班做PPT 半年挣8万是什么感觉 你好 我是佳佳 一个用PPT兼职挣钱的宝妈 我现在每天抽2个小时 坐在电脑前 把各种素材像拼图一样拼接一下 像这样 然后把成稿投稿到设计平台 就能挣到钱 你是不是觉得 我是个职业设计师 挺厉害的 不是的
  • NLP(十五)让模型来告诉你文本中的时间

    背景介绍 在文章NLP入门 十一 从文本中提取时间 中 笔者演示了如何利用分词 词性标注的方法从文本中获取时间 当时的想法比较简单快捷 只是利用了词性标注这个功能而已 因此 在某些地方 时间的识别效果并不太好 比如以下的两个例子 原文1 苏
  • python递归实现字符串逆反

    def main string input Enter a string string1 reverse string print string s reverse format is string1 def reverse string
  • YOLOV7学习记录之训练过程

    在前面学习YOLOV7的过程中 我们已经学习了其网络结构 然而实际上YOLOV7项目的难点并不在于其网络模型而是在于其损失函数的设计 即如何才能训练出来合适的bbox 神经网络模型都有训练和测试 推理 过程 在YOLOV7的训练过程中 包含
  • Java学习笔记:Java中的加号“+”

    在今晚学习Java时惊奇地发现Java中有 System out println 赋值后c的值为 c 这样的与c语言不同的语法 本着打破砂锅问到底 xue dao si 的精神 稍微整理了一下 下面是整理出来的Java中加号 的用法 算术运
  • mysql字段使用非int做主键,查询时候使用整型和字符串做查询条件的区别

    where条件key是整型的时候也可以找到记录 但是效率慢 不会使用索引 使用字符串的时候会使用主键索引会很快
  • ionic入门教程第十五课-ionic性能优化之图片延时加载

    周五的时候有个朋友让我写一个关于图片延时加载的教程 直到今天才有空编辑 这阶段真的是很忙 公众号都变成僵尸号了 实在是对不起大家 有人喜欢我的教程 可能我总习惯了用比较简单容易理解的方式去描述这些东西 别的就不多说了 大家遇到什么问题 可以
  • 100天精通Python(基础篇)——第23天:while循环 :99乘法表

    i 0 while i lt 10 print 我喜欢你 i 1 print endl i 0 sum 0 while i lt 101 i 1 sum i print f sum sum import random num random
  • django1.10 静态文件配置

    settings配置 网站引用静态文件时都会加上该地址 如 http www xxx com static css mini css STATIC URL static 静态文件根目录 执行命令 python manage py colle
  • PostgreSQL 服务启动不了问题

    配置了postgresql数据的配置文件 pg hba conf后 重记一下服务 结果启动不了 提 示错误 root instance 609xznso run systemctl start postgresql 11 Job for p
  • C++11 function、bind、可变参数模板

    在设计回调函数的时候 无可避免地会接触到可回调对象 在C 11中 提供了std function和 std bind两个方法来对可回调对象进行统一和封装 C 语言中有几种可调用对象 函数 函数指针 lambda表达式 bind创建的对象以及
  • Hibernate的加载方式——GET与LOAD的对比

    在Hibernate框架中 最常用到的加载方式就非Get和Load莫属了 然而Get和Load在加载方式上边还有很多的不同 下面让我们来分析一下他们的不同之处 区别 从返回的结果上来看 get load在检索到数据的时候 会返回对象 代理对
  • firefox火狐书签windows和ubuntu无法同步问题

    装了ubuntu后发现firefox的书签没法同步 最终发现问题的原因 firefox有个全球服务和本地服务 ubuntu下的firefox默认是全球服务的 而windows下的firefox默认是本地服务的 这样相当于两个系统下默认的存储
  • 【生信】初探基因定位和全基因组关联分析

    初探QTL和GWAS 文章目录 初探QTL和GWAS 实验目的 实验内容 实验题目 第一题 玉米MAGIC群体的QTL分析 第二题 TASSEL自带数据集的关联分析 实验过程 玉米MAGIC群体的QTL分析 包含的数据 绘制LOD曲线 株高
  • PyTorch训练深度卷积生成对抗网络DCGAN

    文章目录 DCGAN介绍 代码 结果 参考 DCGAN介绍 将CNN和GAN结合起来 把监督学习和无监督学习结合起来 具体解释可以参见 深度卷积对抗生成网络 DCGAN DCGAN的生成器结构 图片来源 https arxiv org ab
  • PCA(主成分分析方法)

    目录 1 降维问题 2 向量与基变换 2 1 内积与投影 2 2 基 2 3 基变换的矩阵 3 协方差矩阵及优化目标 3 1 方差 3 2 协方差 3 3 协方差矩阵 3 4 协方差矩阵对角化 4 算法与实例 4 1 PCA算法 4 2 实
  • postgres数据库进行备份和恢复

    参考 http blog chinaunix net uid 354915 id 3504632 html root localhost postgres 9 3 5 pg dump help pg dump dumps a databas