美文网首页
2019-01-26Postgresql备份与还原命令pg_du

2019-01-26Postgresql备份与还原命令pg_du

作者: steelingyu | 来源:发表于2019-01-26 15:40 被阅读0次

postgresql数据库的备份和还原命令pg_dump

常用命令:

备份:

pg_dump -U postgres -d myDBname -f dump.sql

1    pg_dump -U postgres -d myDBname -f dump.sql

其中

postgres是用户名

myDBname是数据库名

dump.sql是文件名

还原:

createdb newDBname

psql -d newDBname -U postgres -f dump.sql

1    createdb newDBname

2    psql -d newDBname -U postgres -f dump.sql

其中

postgres是用户名

newDBname是数据库名

dump.sql是文件名

参考:

pg_dump 把一个数据库转储为纯文本文件或者是其它格式.

用法:

  pg_dump [选项]... [数据库名字]

一般选项:

  -f, --file=FILENAME          输出文件或目录名

  -F, --format=c|d|t|p        输出文件格式 (定制, 目录, tar)

                              明文 (默认值))

  -j, --jobs=NUM              执行多个并行任务进行备份转储工作

  -v, --verbose                详细模式

  -V, --version                输出版本信息,然后退出

  -Z, --compress=0-9          被压缩格式的压缩级别

  --lock-wait-timeout=TIMEOUT  在等待表锁超时后操作失败

  -?, --help                  显示此帮助, 然后退出

控制输出内容选项:

  -a, --data-only              只转储数据,不包括模式

  -b, --blobs                  在转储中包括大对象

  -c, --clean                  在重新创建之前,先清除(删除)数据库对象

  -C, --create                在转储中包括命令,以便创建数据库

  -E, --encoding=ENCODING      转储以ENCODING形式编码的数据

  -n, --schema=SCHEMA          只转储指定名称的模式

  -N, --exclude-schema=SCHEMA  不转储已命名的模式

  -o, --oids                  在转储中包括 OID

  -O, --no-owner              在明文格式中, 忽略恢复对象所属者

  -s, --schema-only            只转储模式, 不包括数据

  -S, --superuser=NAME        在明文格式中使用指定的超级用户名

  -t, --table=TABLE            只转储指定名称的表

  -T, --exclude-table=TABLE    不转储指定名称的表

  -x, --no-privileges          不要转储权限 (grant/revoke)

  --binary-upgrade            只能由升级工具使用

  --column-inserts            以带有列名的INSERT命令形式转储数据

  --disable-dollar-quoting    取消美元 (符号) 引号, 使用 SQL 标准引号

  --disable-triggers          在只恢复数据的过程中禁用触发器

  --enable-row-security        启用行安全性(只转储用户能够访问的内容)

  --exclude-table-data=TABLE  不转储指定名称的表中的数据

  --if-exists              当删除对象时使用IF EXISTS

  --inserts                    以INSERT命令,而不是COPY命令的形式转储数据

  --no-security-labels        不转储安全标签的分配

  --no-synchronized-snapshots  在并行工作集中不使用同步快照

  --no-tablespaces            不转储表空间分配信息

  --no-unlogged-table-data    不转储没有日志的表数据

  --quote-all-identifiers      所有标识符加引号,即使不是关键字

  --section=SECTION            备份命名的节 (数据前, 数据, 及 数据后)

  --serializable-deferrable  等到备份可以无异常运行

  --snapshot=SNAPSHOT          为转储使用给定的快照

  --strict-names              要求每个表和/或schema包括模式以匹配至少一个实体

  --use-set-session-authorization

                              使用 SESSION AUTHORIZATION 命令代替

                ALTER OWNER 命令来设置所有权

联接选项:

  -d, --dbname=DBNAME      对数据库 DBNAME备份

  -h, --host=主机名        数据库服务器的主机名或套接字目录

  -p, --port=端口号        数据库服务器的端口号

  -U, --username=名字      以指定的数据库用户联接

  -w, --no-password        永远不提示输入口令

  -W, --password          强制口令提示 (自动)

  --role=ROLENAME          在转储前运行SET ROLE

如果没有提供数据库名字, 那么使用 PGDATABASE 环境变量

的数值.

报告错误至 <pgsql-bugs@postgresql.org>.

---------------------

作者:Morpheus丶

来源:CSDN

原文:https://blog.csdn.net/timo1160139211/article/details/78171272

 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 行记录)

相关文章

网友评论

      本文标题:2019-01-26Postgresql备份与还原命令pg_du

      本文链接:https://www.haomeiwen.com/subject/hguijqtx.html