How does one drop a template database from PostgreSQL?

PostgresqlPostgresql 9.1Database Template

Postgresql Problem Overview


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.

Postgresql Solutions


Solution 1 - Postgresql

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

Solution 2 - Postgresql

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

Solution 3 - Postgresql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondbkaplunView Question on Stackoverflow
Solution 1 - PostgresqldbkaplunView Answer on Stackoverflow
Solution 2 - PostgresqlVynlJunkieView Answer on Stackoverflow
Solution 3 - PostgresqldbaView Answer on Stackoverflow