数据库(pgsql)进行数据库备份,在进行【数据修复,数据调整】时,回滚的策略

一、可连接,有执行权限的数据库连接后

-- 备份
select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname = 'your_table';
drop database if exists your_table_backup;
create database your_table_backup template your_table;

-- 执行数据库操作
update table xxxx set (xxxxxxx) where. id = 1
insert into xxx (xxxxx)
delete from xxxxx


-- 还原
select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname = 'your_table_backup';
select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname = 'your_table';
drop database if exists your_table;
create database your_table template your_table_backup;

相关的解释:

断开数据库的所有连接:select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname = 'your_table';

判断备份数据库是否存在,存在删除后备份,不存在以原数据库为模版,新建数据库
drop database if exists your_table_backup;
create database your_table_backup template your_table;

posted @ 2023-07-21 14:53  代码红了一大片  阅读(22)  评论(0编辑  收藏  举报