如何从PostgreSQL中删除模板数据库?

64
4个回答

89
postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';
UPDATE 1
postgres=# DROP DATABASE template_postgis;
DROP DATABASE
postgres=# 

我查看了创建模板的脚本,发现其中有这样一行代码:psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" - dbkaplun
3
这会引起更多的问题。我在Ubuntu上从9.1升级到了9.2版本,这隐式地创建了template1。然后我执行了你的命令,看起来它是有效的。然而,现在我安装了phpPgAdmin,但我无法登录,它显示:“FATAL: database "template1" does not exist”。 - hek2mgl
1
重新创建template1: create database template1 template template0; UPDATE pg_database SET datistemplate='true' WHERE datname='template1'; 参考链接:http://pgsql.inb4.se/2009/april/rebuild-template1.html - Sam Watkins

48
你可以使用"alter database"命令。比改变元数据更简单、更安全。
postgres=# create database tempDB is_template true;
CREATE DATABASE
postgres=# drop database tempDB;
ERROR:  cannot drop a template database
postgres=# alter database tempDB is_template false;
ALTER DATABASE
postgres=# drop database tempDB;
DROP DATABASE
postgres=# 

文档


2
这个解决方案可行且比所选方案简单得多。 - Brad
@amoe 很好的发现。已编辑添加文档链接。 - VynlJunkie
1
谢天谢地你存在啊,伙计。其他的解决方案都没用。谢谢! - koullislp
2
这个可以运行,但请确保在 'false' 周围添加单引号 POSTGRES v10 - Luis Villavicencio

0
除了其他答案之外,您可以使用以下SQL语句删除PostgreSQL默认的template0template1数据库:
ALTER DATABASE template0 IS_TEMPLATE FALSE;
DROP DATABASE template0;

ALTER DATABASE template1 IS_TEMPLATE FALSE;
DROP DATABASE template1;

-1
update pg_database set datistemplate=false where datname='template0';
update pg_database set datistemplate=false where datname='template1';

....

Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok
Checking for hash indexes                                   ok

Upgrade Complete
----------------

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接