PostgreSQL单个表误删除进行恢复

PostgreSQL单个表误删除进行恢复

最后编辑时间:2022年1月23日00:09:24

恢复步骤:
1)首先找到最近的备份文件(日常备份必不可少)
2)创建一个新的数据库,并用备份文件进行恢复
3)在新的数据库中将目标表的数据进行导出
4)将该表数据进行恢复
以下为测试示例:

[postgres@rhel6wcb ~]$ pg_dump -h 127.0.0.1 -p 5432 -d test -Fc -f pg_dump.dmp
[postgres@rhel6wcb ~]$ ls -lth
total 35M
-rw-rw-r-- 1 postgres postgres 35M Dec  2 22:23 pg_dump.dmp
[postgres@rhel6wcb ~]$ psql
psql (11.7)
Type "help" for help.
postgres@postgres=>\c test
You are now connected to database "test" as user "postgres".
postgres@test=>\dt+ test_big1
                      List of relations
 Schema |   Name    | Type  |  Owner   |  Size  | Description 
--------+-----------+-------+----------+--------+-------------
 public | test_big1 | table | postgres | 280 MB | 
(1 row)
postgres@test=>select count(*) from test_big1;
  count  
---------
 5000000
(1 row)
postgres@test=>drop table test_big1;
DROP TABLE


postgres@postgres=>create database test2;
CREATE DATABASE
[postgres@rhel6wcb ~]$ pg_restore -h 127.0.0.1 -p 5432 -d test2 pg_dump.dmp
[postgres@rhel6wcb ~]$ psql
psql (11.7)
Type "help" for help.

postgres@postgres=>\c test2
You are now connected to database "test2" as user "postgres".
postgres@test2=>\dt+ test_big1;
                      List of relations
 Schema |   Name    | Type  |  Owner   |  Size  | Description 
--------+-----------+-------+----------+--------+-------------
 public | test_big1 | table | postgres | 280 MB | 
(1 row)
[postgres@rhel6wcb ~]$ pg_dump -h 127.0.0.1 -p 5432 -d test2 -t test_big1 -Fp -f test_big1.sql
[postgres@rhel6wcb ~]$ ls -lht
total 261M
-rw-rw-r-- 1 postgres postgres 227M Dec  2 22:27 test_big1.sql
-rw-rw-r-- 1 postgres postgres  35M Dec  2 22:23 pg_dump.dmp
[postgres@rhel6wcb ~]$ psql test <test_big1.sql
SET
SET
SET
SET
SET
 set_config 
------------
 
(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 5000000
CREATE INDEX
[postgres@rhel6wcb ~]$ psql
psql (11.7)
Type "help" for help.

postgres@postgres=>\c test
You are now connected to database "test" as user "postgres".
postgres@test=>\dt+ test_big1;
                      List of relations
 Schema |   Name    | Type  |  Owner   |  Size  | Description 
--------+-----------+-------+----------+--------+-------------
 public | test_big1 | table | postgres | 280 MB | 
(1 row)
postgres@test=>select count(*) from test_big1;
  count  
---------
 5000000
(1 row)

到此误删除的表的数据已经恢复。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值