Skip to main content
0 reactions

template0 vs template1 in PostgreSQL

Card for template0 vs template1 in PostgreSQL: template1 is the default, copies your changes and needs the same encoding; template0 is pristine, never changed and allows any encoding.
template0 vs template1 in PostgreSQL.

Every PostgreSQL server has two databases you never created: template0 and template1. They’re templates: CREATE DATABASE makes a new database by copying one of them. Here’s the difference between template0 and template1, and when to choose each.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
CREATE DATABASE name TEMPLATE template0;
  • Without TEMPLATE, CREATE DATABASE copies template1.
  • TEMPLATE template0 copies template0 instead.

See Template Databases (opens in a new tab) in the PostgreSQL documentation.

The Difference at a Glance

template1template0
Copied by CREATE DATABASEBy defaultOnly with TEMPLATE template0
Can you change it?Yes, and every database you create later gets the changeNo: it’s meant never to change, and you can’t even connect to it
Encoding and locale of the copyThe same as template1Any you choose

Step 1: See That New Databases Copy template1

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

To see it for yourself, add a small table to template1; the Clean Up at the end removes it again. Connect to template1:

postgres=#Plain text
\c template1
OutputPlain text
You are now connected to database "template1" as user "postgres".
template1=#SQL
CREATE TABLE welcome (message text);
OutputPlain text
CREATE TABLE

Go back to postgres and create two databases, one from each template:

template1=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
CREATE DATABASE copy_one;
OutputPlain text
CREATE DATABASE
postgres=#SQL
CREATE DATABASE copy_zero TEMPLATE template0;
OutputPlain text
CREATE DATABASE

Connect to copy_one, made from template1, and list its tables:

postgres=#Plain text
\c copy_one
OutputPlain text
You are now connected to database "copy_one" as user "postgres".
copy_one=#Plain text
\dt
OutputPlain text
           List of tables
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | welcome | table | postgres
(1 row)

It has the welcome table. Now do the same in copy_zero, made from template0:

copy_one=#Plain text
\c copy_zero
OutputPlain text
You are now connected to database "copy_zero" as user "postgres".
copy_zero=#Plain text
\dt
OutputPlain text
Did not find any tables.

No tables: template0 never got the welcome table.

Step 2: See Why template0 Stays Pristine

You can’t connect to template0 at all:

copy_zero=#Plain text
\c template0
OutputPlain text
connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "template0" is not currently accepting connections
Previous connection kept

template0 is marked to refuse connections (its datallowconn flag is false), so nobody can change it by accident, as the documentation (opens in a new tab) explains. That’s what keeps it a clean copy of what a new server starts with.

Step 3: Use template0 for a Different Encoding

A copy of template1 must keep template1’s encoding. Go back to postgres:

copy_zero=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".

Ask for another encoding, and PostgreSQL refuses and suggests template0:

postgres=#SQL
CREATE DATABASE latin_db ENCODING 'LATIN1';
OutputPlain text
ERROR:  new encoding (LATIN1) is incompatible with the encoding of the template database (UTF8)
HINT:  Use the same encoding as in the template database, or use template0 as template.

With TEMPLATE template0, it works:

postgres=#SQL
CREATE DATABASE latin_db ENCODING 'LATIN1' TEMPLATE template0;
OutputPlain text
CREATE DATABASE

Note: This server uses the C locale, which works with any encoding. With another locale, PostgreSQL also checks that the encoding matches it, as CREATE DATABASE (opens in a new tab) explains.

Clean Up

Remove the welcome table from template1, so the databases you create later don’t get it:

postgres=#Plain text
\c template1
OutputPlain text
You are now connected to database "template1" as user "postgres".
template1=#SQL
DROP TABLE welcome;
OutputPlain text
DROP TABLE

Then go back to postgres and drop the three example databases:

template1=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE copy_one;
OutputPlain text
DROP DATABASE
postgres=#SQL
DROP DATABASE copy_zero;
OutputPlain text
DROP DATABASE
postgres=#SQL
DROP DATABASE latin_db;
OutputPlain text
DROP DATABASE

Common Mistakes

MistakeWhat happensDo this instead
Leaving test objects in template1Every database you create afterwards gets them too.Drop them from template1, or create the database from template0.
Connecting to template0FATAL: database "template0" is not currently accepting connectionsLeave template0 alone, and use TEMPLATE template0 in CREATE DATABASE instead.
A different encoding without template0ERROR: new encoding (LATIN1) is incompatible with the encoding of the template database (UTF8)Add TEMPLATE template0.

FAQ

Why does restoring a backup use template0?

A backup made with pg_dump should be restored into a pristine database, so nothing added to template1 later gets in the way. The documentation (opens in a new tab) recommends template0 for exactly that.

Can I drop template1?

You can, and then recreate it from template0, for example after adding things to it by mistake. It must first be marked as no longer a template. Most people never need to.

What about the postgres database?

It’s created along with the two templates, as the default database for users and apps to connect to. It’s simply a copy of template1, and it can be recreated if necessary.

Conclusion

CREATE DATABASE copies template1, so changes to it reach every new database. Choose template0 when you need a pristine copy, for example to restore a backup, or a different encoding.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments