暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

postgresql如何防止误删库

原创 进击的CJR 2022-10-23
1456

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
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论