Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one drop a template database from PostgreSQL?

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.

like image 651
dbkaplun Avatar asked Jul 09 '12 03:07

dbkaplun


People also ask

How do I drop a PostgreSQL database?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.

Can I drop default Postgres database?

Basically - no. postgres database is here as a non-template database with reasonable guarantee that it exists - so any script that doesn't know where to connect to, can connect there. if you will remove template1 - you will lose the ability to create new databases (at least easily).

What is Postgres template database?

The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.


2 Answers

postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';
UPDATE 1
postgres=# DROP DATABASE template_postgis;
DROP DATABASE
postgres=# 
like image 119
dbkaplun Avatar answered Oct 01 '22 23:10

dbkaplun


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

like image 36
VynlJunkie Avatar answered Oct 01 '22 21:10

VynlJunkie