63
postgres=# DROP DATABASE template_postgis;
ERROR:  cannot drop a template database

http://www.postgresql.org/docs/9.1/static/manage-ag-templatedbs.html makes it seem like if I set template_postgis.datistemplate = false, I'll be able to drop it, but I don't know how to set that.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
dbkaplun
  • 3,407
  • 2
  • 26
  • 33

4 Answers4

88
postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';
UPDATE 1
postgres=# DROP DATABASE template_postgis;
DROP DATABASE
postgres=# 
dbkaplun
  • 3,407
  • 2
  • 26
  • 33
  • I looked in the script I used that created the template. There was the line `psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"`. – dbkaplun Jul 09 '12 at 03:35
  • 3
    This causes further problems. I made an upgrade from 9.1 -> 9.2 on ubuntu. This created template1 implicitly. I then executed your command and it seems to work. However, now I installed phpPgAdmin and I cannot login, it says : 'FATAL: database "template1" does not exist' – hek2mgl Jan 13 '14 at 11:47
  • 1
    To re-create template1: `create database template1 template template0;` `UPDATE pg_database SET datistemplate='true' WHERE datname='template1';` See also: http://pgsql.inb4.se/2009/april/rebuild-template1.html – Sam Watkins Jul 30 '15 at 06:00
47

You can use the alter database command. Much simpler and safer than upsetting metadata.

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=# 

Documentation

VynlJunkie
  • 1,953
  • 22
  • 26
0

In addition to other answers, you can drop template0 and template1 databases which PostgreSQL has by default with the SQLs below:

ALTER DATABASE template0 IS_TEMPLATE FALSE;
DROP DATABASE template0;
ALTER DATABASE template1 IS_TEMPLATE FALSE;
DROP DATABASE template1;
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
-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
----------------
DieterDP
  • 4,039
  • 2
  • 29
  • 38
dba
  • 1