Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need template0 and template1 in PostgreSQL?

Tags:

I'm a beginner in PostgreSQL. I wonder why the \l command in psql shows databases template0 and template1.

I searched the web but unfortunately didn't find the right resources. But I did find that after removing both (template0 & template1) we can't create new databases any more.

like image 806
Ruth-The Glix Avatar asked Jul 01 '18 09:07

Ruth-The Glix


People also ask

What is template1?

A template is a form, mold or pattern used as a guide to make something. Here are some examples of templates: Website design. Creating a document. Knitting a sweater.

What are PostgreSQL templates?

Whenever you create a new database in Postgres, you are actually basing it off an already present database in your cluster. This database, template1 , and another, called template0 , are standard system databases that exist in every newly created database cluster.

How do I allocate memory to PostgreSQL?

shared_buffers (integer) The shared_buffers parameter determines how much memory is dedicated to the server for caching data. The value should be set to 15% to 25% of the machine's total RAM. For example: if your machine's RAM size is 32 GB, then the recommended value for shared_buffers is 8 GB.


1 Answers

As the names indicate, those are template databases for creating new databases.

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.

template0 starts out being the same and should never be changed - to provide a virgin template with original settings.

Their role is described in detail in the chapter "Template Databases" in the manual.

But you can use any database of the same cluster as template with the TEMPLATE keyword - as long as there are no open connections to it. This is useful to populate new databases quickly. See:

  • How to clone a test database from a production one in one single action?

  • Shell script to execute pgsql commands in files

  • Truncating all tables in a Postgres database

like image 137
Erwin Brandstetter Avatar answered Oct 14 '22 19:10

Erwin Brandstetter