《转》postgresql数据库备份、恢复

转发:https://blog.csdn.net/niuxinzan/article/details/17243103
转发:https://www.cnblogs.com/BillyYoung/p/11057854.html

一、数据库备份

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


详解  
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
postgresql(简称Postgres)是一个高性能的企业级开源关系型数据库管理系统。在实际运行过程中,由于各种原因,Postgres数据库可能会出现一些问题,比如数据损坏、硬盘故障、误删除等情况,这时候备份和恢复就显得尤为重要。 Postgres数据库备份 Postgres数据库备份主要包括两种方式:物理备份和逻辑备份。 1. 物理备份 物理备份是指备份整个Postgres数据库的物理文件,包括数据和日志等信息。这种备份方式具有非常高的恢复效率和完整性,备份后可以快速地还原到任何一个时间点的状态。但是,物理备份存在一些限制,例如,如果备份时数据库正在运行,会存在锁定文件的问题,同时备份后的数据不太容易人工查看和修改。 实现物理备份的两种方式: (1)基于文件系统备份 这种备份方式是直接备份Postgres数据库的文件目录,包括数据文件(Data File)、事务日志文件(WAL File)和配置文件等。使用类似于cp、tar等常见的文件操作命令完成备份,简单方便。 (2)基于pg_basebackup工具备份 pg_basebackup是PostgreSQL自带的备份工具,可以很方便地进行物理备份。只需指定备份目录,即可将整个PostgreSQL数据库备份到指定目录下。 2. 逻辑备份 逻辑备份是指备份Postgres数据库中的逻辑数据,比如表、视图、函数、触发器等,备份后数据可以人工查看和编辑。但是,逻辑备份的恢复效率没有物理备份高,同时在备份和恢复过程中需要注意一些事项。 实现逻辑备份的两种方式: (1)基于pg_dump工具备份 pg_dump工具是PostgreSQL自带的备份工具,可以对数据库进行逻辑备份。备份时可以指定备份的对象(比如表、视图、函数等),也可以备份整个数据库。备份完成后,可以通过pg_restore工具进行恢复。 (2)基于导出/导入工具备份 除了pg_dump工具,还有其他的导出/导入工具可以进行逻辑备份,比如psql、SQL Shell等。其中,psql是PostgreSQL自带的终端用户控制台工具,可以对数据库进行交互式的管理和操作,包括导出/导入数据等。 Postgres数据库恢复 Postgres数据库恢复需要根据备份类型进行相应的恢复操作。 1. 对于物理备份 (1)基于文件系统备份的恢复 只需要将备份文件还原到需要恢复PostgreSQL数据库目录下即可,恢复后启动PostgreSQL即可。 (2)基于pg_basebackup工具备份的恢复 指定备份目录为数据目录,启动PostgreSQL即可。在恢复完成后,还可以通过pg_archivecleanup工具清理归档日志。 2. 对于逻辑备份 (1)基于pg_dump工具备份的恢复 首先需要创建一个新的数据库(如果原数据库已经损坏)。然后使用pg_restore工具将备份文件恢复到指定数据库中即可。 (2)基于导出/导入工具备份的恢复pg_dump工具类似,需要先创建一个新的数据库,然后使用导入工具恢复备份文件到指定数据库中。 总结 Postgres数据库备份恢复是保证数据安全、保障系统稳定的重要手段。无论是物理备份还是逻辑备份,都有各自的优势和限制,需要根据实际需求进行选择和应用。同时,备份和恢复操作也需要密切结合数据库实际情况和业务需求,综合考虑一些因素,如备份周期、备份策略、备份类型、备份存储空间、恢复时间等,才能够达到最佳效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值