悟夜叉个人博客 其他分类 postgresql如何防止误删库

postgresql如何防止误删库

pg模板库

template1和template0是PostgreSQL的模板数据库。所谓模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件。PostgreSQL安装好以后会默认附带两个模板数据库:template0和template1。

设置模板库属性

pg_database的datistemplate字段可以表明该库是否是模板库

postgres=# select * from pg_database;
oid | datname | datdba | encoding | datcollate | datctype | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl
-------±----------±-------±---------±-----------±-----------±--------------±-------------±-------------±--------------±-------------±-----------±--------------±-------------------------------------------------------
1 | template1 | 10 | 6 | en_US.utf8 | en_US.utf8 | t | t | -1 | 13592 | 480 | 1 | 1663 | {=c/postgres,postgres=CTc/postgres}
13592 | template0 | 10 | 6 | en_US.utf8 | en_US.utf8 | t | f | -1 | 13592 | 480 | 1 | 1663 | {=c/postgres,postgres=CTc/postgres}
16660 | db01 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 16659 |
16696 | osdba2 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16697 | osdba3 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16698 | osdba4 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16699 | mydb3 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16686 | mydb | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 16685 | {=Tc/postgres,postgres=CTc/postgres}
13593 | postgres | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 | {postgres=CTc/postgres,user2=c/postgres,=CTc/postgres}
(9 rows)

模板库防止误删

pg中是无法删除模板数据库的,改一下pg_database的datistemplate字段即可防止误删数据库的情况发生。

postgres=# update pg_database set datistemplate = ‘true’ where datname = ‘mydb’;
UPDATE 1
postgres=# select * from pg_database;
oid | datname | datdba | encoding | datcollate | datctype | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl
-------±----------±-------±---------±-----------±-----------±--------------±-------------±-------------±--------------±-------------±-----------±--------------±-------------------------------------------------------
1 | template1 | 10 | 6 | en_US.utf8 | en_US.utf8 | t | t | -1 | 13592 | 480 | 1 | 1663 | {=c/postgres,postgres=CTc/postgres}
13592 | template0 | 10 | 6 | en_US.utf8 | en_US.utf8 | t | f | -1 | 13592 | 480 | 1 | 1663 | {=c/postgres,postgres=CTc/postgres}
16660 | db01 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 16659 |
16696 | osdba2 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16697 | osdba3 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16698 | osdba4 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
16699 | mydb3 | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 |
13593 | postgres | 10 | 6 | en_US.utf8 | en_US.utf8 | f | t | -1 | 13592 | 480 | 1 | 1663 | {postgres=CTc/postgres,user2=c/postgres,=CTc/postgres}
16686 | mydb | 10 | 6 | en_US.utf8 | en_US.utf8 | t | t | -1 | 13592 | 480 | 1 | 16685 | {=Tc/postgres,postgres=CTc/postgres}
(9 rows)

postgres=# drop database mydb;
ERROR: cannot drop a template database

误删保护解除

如果确认删除该库后,可以将属性datistemplate设置为f,则可以删除成功

postgres=# update pg_database set datistemplate = ‘false’ where datname = ‘mydb’;
UPDATE 1
postgres=# drop database mydb;
DROP DATABAS

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

站长QQ/VX:82794

在线咨询: QQ交谈

邮箱: 82794@qq.com

任何技术问题请联系QQ,非特殊行业请勿加微信!龙信小伙伴请联系微信群找我。
关注微信
非商务合作请勿添加

非商务合作请勿添加微信

返回顶部