PostgreSQL数据库备份与恢复之pg_dump简单体验

21610阅读 1评论2015-02-07 jackson198574
分类:Mysql/postgreSQL

  





 PostgreSQL数据库的简单导入、导出操作体验,操作还是非常简单易用的。当然该工具还提供了众多的参数,以供不同的情景下使用,在此我们只做最简单的导出和导入操作。

创建测试用表:
postgres=# \c music
您现在已经连线到数据库 "music",用户 "postgres".


music=#  create table test(ID int);
CREATE TABLE
music=# insert into test values(1);
INSERT 0 1
music=# insert into test values(2);
INSERT 0 1
music=# insert into test values(3);
INSERT 0 1

验证数据插入成功:
music=# select * from test;
 id 
----
  1
  2
  3
(3 行记录)


music=# \d
              关联列表
 架构模式 | 名称 |  型别  |  拥有者  
----------+------+--------+----------
 public   | test | 资料表 | postgres       -------查看表信息
(1 行记录)




备份数据库:
-bash-3.2$ pg_dump music >/tmp/music.dmp

查看备份:

-bash-3.2$ cd /tmp/
-bash-3.2$ ls
db2.tmp.5072  dmp  keyring-BJJFsF  mapping-root  music.dmp  PostgreSQL9.4_PACK  scim-panel-socket:0-root  vmware-tools-distrib




删除数据库:
postgres=# drop database music;
DROP DATABASE
postgres=# \l
                                     资料库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 行记录)

从上面信息看,music数据库已删除!~~~



恢复数据库:

postgres=# create database music;
CREATE DATABASE

开始恢复:
$ psql music
SET
SET
SET
SET
SET
SET
CREATE EXTENSION
COMMENT
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 3
REVOKE
REVOKE
GRANT
GRANT
-bash-3.2$ 
-bash-3.2$ 
-bash-3.2$ psql
psql (9.4.0)
输入 "help" 来获取帮助信息.






验证是否恢复数据库:
postgres=# \l
                                     资料库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
-----------+----------+----------+-------------+-------------+-----------------------
 music     | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 行记录)




连接数据库:
postgres=# \c music
您现在已经连线到数据库 "music",用户 "postgres".

music=# \d
              关联列表
 架构模式 | 名称 |  型别  |  拥有者  
----------+------+--------+----------
 public   | test | 资料表 | postgres   ---表已恢复过来!
(1 行记录)



验证是否已把数据恢复过来:
music=# select * from test;   ----查看数据存在否
 id 
----
  1
  2
  3
(3 行记录)




恢复成功!


上一篇:玩转Oracle之12c 可插拔数据库数据泵功能体验
下一篇:PostgreSQL数据库在线备份配置

文章评论