PostgreSQL删除与创建模板数据库

版权声明:本文为博主原创之文章,未经博主允许谢绝转载。 https://blog.csdn.net/pg_hgdb/article/details/79959476

首先查看目前存在的数据库。

highgo=# \l
                          List of databases
   Name    | Owner  | Encoding | Collate | Ctype | Access privileges 
-----------+--------+----------+---------+-------+-------------------
 highgo    | highgo | UTF8     | C       | C     | 
 template0 | highgo | UTF8     | C       | C     | =c/highgo        +
           |        |          |         |       | highgo=CTc/highgo
 template1 | highgo | UTF8     | C       | C     | =c/highgo        +
           |        |          |         |       | highgo=CTc/highgo
(3 rows)

highgo=

尝试删除数据库。

highgo=# drop database template1; 
ERROR:  42809: cannot drop a template database

提示禁止删除一个模板数据库。
查询该数据库的datistemplate项。

highgo=# select datname ,datistemplate from pg_database where datname='template1';
  datname  | datistemplate 
-----------+---------------
 template1 | t
(1 row)

更新该值为假。

highgo=# UPDATE pg_database SET datistemplate=false where datname='template1';
UPDATE 1

再次查询。

highgo=# select datname ,datistemplate from pg_database where datname='template1';
  datname  | datistemplate 
-----------+---------------
 template1 | f
(1 row)

删除数据库。


highgo=# drop database template1; 
DROP DATABASE

以highgo数据库为模板创建数据库tempbak。


highgo=# create database tempbak template highgo;      
CREATE DATABASE

查询该数据库的datistemplate项。

highgo=# select datname ,datistemplate from pg_database where datname='tempbak';
 datname | datistemplate 
---------+---------------
 tempbak | f
(1 row)

更新该列。

highgo=# UPDATE pg_database SET datistemplate=true WHERE datname='tempbak';
UPDATE 1
highgo=#  select datname ,datistemplate from pg_database where datname='tempbak';
 datname | datistemplate 
---------+---------------
 tempbak | t
(1 row)
highgo=# \l
                          List of databases
   Name    | Owner  | Encoding | Collate | Ctype | Access privileges 
-----------+--------+----------+---------+-------+-------------------
 highgo    | highgo | UTF8     | C       | C     | 
 tempbak   | highgo | UTF8     | C       | C     | 
 template0 | highgo | UTF8     | C       | C     | =c/highgo        +
           |        |          |         |       | highgo=CTc/highgo
(3 rows)

尝试删除该数据库。

highgo=# drop database tempbak;
ERROR:  42809: cannot drop a template database
highgo=# 

 







By徐云鹤

阅读更多 登录后自动展开

扫码向博主提问

PostgreSQL_HighGoDB

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • POSTGRESQL
  • HIGHGO
去开通我的Chat快问
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页