Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template0 and template1 database dropped accidently

Tags:

postgresql

I did not know that template0 and template1 database templates are required in order to create empty databases. I deleted them in order to clear up postgres. Now I'm not able to create any new database. Gives me this error :

ERROR:  template database "template1" does not exist

What can I do to get things going once again. I'll be very thankful for any help.

like image 912
Dangling Cruze Avatar asked Jan 16 '15 20:01

Dangling Cruze


People also ask

What is template0 and template1 in PostgreSQL?

“template0” is also a template database. It contains the same default database objects as “template1,” except you shouldn't modify it because it's meant to be a pristine database copy and ICD for PostgreSQL prevents you from doing so.

What is template1?

template1 is the one used by default. You can alter / add / remove objects there to affect every newly created DB. CREATE DATABASE basically makes a copy of it on the file level (very fast) to create a new instance.

What is a template database?

An Access template is a file that, when opened, creates a complete database application. The database is ready to use, and contains all the tables, forms, reports, queries, macros, and relationships that you need to start working.

Which database acts as a template for creating new database?

datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE . If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it.


2 Answers

Luckily I had postgres database preserved because it was required for the postgres user to log into psql. Thus, created a template0 and template1 database :

create database template0 TEMPLATE postgres;

and same for template1. Then executed :

update pg_database set datistemplate=true  where datname='template0';

for both databases to stop myself from accidentally deleting these templates again.

Everything works fine now :)

like image 125
Dangling Cruze Avatar answered Sep 18 '22 20:09

Dangling Cruze


On my CentOS 7 box, I was not so lucky as to still have a database to connect to. However:

su postgres -c "initdb /var/lib/pgsql/data"

(as root) created a template0 and template 1 to work with.

like image 22
Jack Simth Avatar answered Sep 17 '22 20:09

Jack Simth