备份 PG 数据库生成的文件可以有两种,一种是 SQL 文件,一种是二进制文件,二进制文件只能使用 pg_restore 进行恢复。

PostgreSQL 数据库操作简要说明

PostgreSQL数据库版本

psql --version
psql (PostgreSQL) 9.1.3

下面是在linux下的操作,在windows下面将su -postgres 换为

运行输入cmd→d:→

cd D:\Program Files\PostgreSQL\9.2\bin,下面创建表、删除表时,需要在postgres下操作,因此需要在创建删除数据库前面输入psql -u postgres

一、数据库备份

1、备份数据库结构

su - postgres
pg_dump -Fc -s -f testdbschema.sql testdb

2、备份数据库数据

su - postgres
pg_dump -Fc -a -f testdbdata.sql testdb

3、备份数据库结构和数据

su - postgres
pg_dump -Fc -f testdbschemadata.sql testdb

4、备份数据库中指定表结构

pg_dump -Fc -s -t citycode -f citycode_schema.sql testdb

5、备份数据库中指定表数据

pg_dump -Fc -a -t citycode -f citycode_data.sql testdb

.6、备份数据库中指定表(结构和数据)

pg_dump -Fc -t citycode -f citycode_schemadata.sql testdb

二、删除数据库

su - postgres

dropdb testdb

三、恢复数据库

1、创建新数据库testdb

su - postgres

createdb testdb;

2、 恢复数据结构(only schema)

su - postgres

pg_restore -s -d testdb testdbschema.sql

3、恢复数据库数据(only data)

su - postgres

pg_restore -a -d testdb testdbdata.sql

4、恢复数据库结构和数据(schema and data)

su - postgres

pg_restore -d testdb testdbschemadata.sql

5、指定表数据恢复

1)删除表

psql testdb

DROP TABLE citycode;

2)恢复表结构

pg_restore -s -t citycode -d testdb citycode_schema.sql

3)恢复表数据

pg_restore -a -t citycode -d testdb citycode_data.sql

4)恢复表(结构和数据)

pg_restore -t citycode -d testdb citycode_schemadata.sql

以上备份恢复相关操作可用于静态(无数据增长)数据库。

重要提示:pg_restore 可用来恢复pg_dump命令以 (Fc\Ft)格式备份的数据文件。执行pg_dump备份命令时若无此格式参数声明,pg_restore 恢复时可能出现错误提示“pg_restore: [archiver] input file does not appear to be a valid archive”。

pg_dump 官方文档

https://www.postgresql.org/docs/10/app-pgdump.html

pg_dumpall

https://www.postgresql.org/docs/10/app-pg-dumpall.html

pg_restore

https://www.postgresql.org/docs/10/app-pgrestore.html

PostgreSQL 数据库备份与恢复 pd_dump pg_restore的更多相关文章

  1. pg_restore - 从一个由 pg_dump 创建的备份文件中恢复 PostgreSQL 数据库。

    SYNOPSIS pg_restore [ option...] [ filename] DESCRIPTION 描述 pg_restore 是一种用于恢复由 pg_dump(1) 创建的任何非纯文本 ...

  2. postgresql数据库的数据导出

    一.pg_dump的用法:数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldump ...

  3. PPAS数据库备份与恢复

    PPAS数据库备份不同于普通的Postgresql数据库的备份,因为PPAS数据库是兼容Oracle数据库的,所以会涉及到同义词.包.存储过程等,这个时候用Postgresql社区的备份与恢复工具时, ...

  4. linux下PostgreSQL数据库的源码安装

    实验环境>>>>>>>>>>>>>>>>>>操作系统:CentOS release 6.3 ...

  5. Linux命令应用大词典-第42章 PostgreSQL数据库

    42.1 initdb:初始化PostgreSQL数据库 42.2 pg_ctl:控制PostgreSQL服务 42.3 psql:PostgreSQL交互式客户端工具 42.4 createdb:创 ...

  6. PostgreSQL数据库常用脚本-初始化、备份、恢复推荐脚本

    公司最近开始逐步推广使用PostgreSQL,为方便开发人员和实施人员操作,特整理数据库初始化.备份.恢复的推荐脚本如下: 1. 连接数据库 psql -h 127.0.0.1 -p 1921 -U ...

  7. pg_dump - 将一个PostgreSQL数据库抽出到一个脚本文件或者其它归档文件中

    SYNOPSIS pg_dump [ option...] [ dbname] DESCRIPTION 描述 pg_dump 是一个用于备份 PostgreSQL 数据库的工具.它甚至可以在数据库正在 ...

  8. PostgreSQL 数据库备份与还原

    PostgreSQL 数据库备份与还原 目录 备份 还原 栗子 备份 PostgreSQL提供的一个工具pg_dump,逻辑导出数据,生成sql文件或其他格式文件,pg_dump是一个客户端工具,可以 ...

  9. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

随机推荐

  1. 基于Java+Selenium的WebUI自动化测试框架(十一)-----读取Excel文件(POI)(1)

    上一篇说了利用JXL的jar包来读取Excel的代码.在Java中,还可以用另外一种jar包来读取Excel的内容,那就是Apache的POI. 这里和之前一样,需要导入POI的jar包,建议导入这三 ...

  2. linux系统编程之进程(五)

    今天继续学习系统编程,学习的主题还是进程,今天主要讨论的是守护进程相关的概念,开始进入正题: 什么是守护进程:       守护进程的创建步骤: 在描述它之前,首先得先了解两个概念:进程组.会话期: ...

  3. Ajax -异步请求 -jquery中ajax分类 -第一层 $.ajax -第二层($.get /$.post) -第三层($.getJson/$.getScript) -相应演示

    Ajax 1.标准请求响应时浏览器的动作(同步操作) 1.1浏览器请求什么资源,跟随显示什么资源2.ajax:异步请求. 2.1局部刷新,通过异步请求,请求到服务器资源数据后,通过脚本修改页面中部分内 ...

  4. 网站检测空链、死链工具(Xenu)

    网站常用检测空链.死链工具,Xenu是很小但是功能强大的检查网站404链接的软件,支持多线程,无需安装可直接打开使用.步骤如下: 网站的链接一般都成千上万,如果存在大量的空链接将大大的影响用户体验,怎 ...

  5. Java - 框架之 MyBites

    一. 开发步骤: 1. 创建 PO (model) 类,根据需求创建. 2. 创建全局配置文件 sqlMapConfig.xml. 3. 编写映射文件. 4. 加载映射文件, 在 SqlMapConf ...

  6. POJ - 3252 - Round Numbers(数位DP)

    链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...

  7. SpringMVC的拦截器和数据校验

    SpringMVC拦截器 什么是拦截器:Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以 ...

  8. [RxJS] Groupby operator

    The use case is similar to Twitter "like" button, you can click "click" button o ...

  9. Python3学习(一)

    基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入内容.#idle中按F5可以运行代 ...

  10. MAC OSX下终端通过NTLM验证,通过代理上网(花了一天时间才解决这个)

    MAC OSX下终端通过NTLM验证,通过代理上网 公司网络限制如下: 公司通过代理来控制内网用户访问外网的权限.用户名和密码为域用户,采用的验证方式是NTLM(用的是foreFront TMG) 遇 ...