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
CREATE DATABASE name TEMPLATE template0;
- Without
TEMPLATE,CREATE DATABASEcopiestemplate1. TEMPLATE template0copiestemplate0instead.
See Template Databases (opens in a new tab) in the PostgreSQL documentation.
The Difference at a Glance
| template1 | template0 | |
|---|---|---|
Copied by CREATE DATABASE | By default | Only with TEMPLATE template0 |
| Can you change it? | Yes, and every database you create later gets the change | No: it’s meant never to change, and you can’t even connect to it |
| Encoding and locale of the copy | The same as template1 | Any 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:
\c template1
You are now connected to database "template1" as user "postgres".
CREATE TABLE welcome (message text);
CREATE TABLE
Go back to postgres and create two databases, one from each template:
\c postgres
You are now connected to database "postgres" as user "postgres".
CREATE DATABASE copy_one;
CREATE DATABASE
CREATE DATABASE copy_zero TEMPLATE template0;
CREATE DATABASE
Connect to copy_one, made from template1, and list its tables:
\c copy_one
You are now connected to database "copy_one" as user "postgres".
\dt
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:
\c copy_zero
You are now connected to database "copy_zero" as user "postgres".
\dt
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:
\c template0
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
Ask for another encoding, and PostgreSQL refuses and suggests template0:
CREATE DATABASE latin_db ENCODING 'LATIN1';
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:
CREATE DATABASE latin_db ENCODING 'LATIN1' TEMPLATE template0;
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:
\c template1
You are now connected to database "template1" as user "postgres".
DROP TABLE welcome;
DROP TABLE
Then go back to postgres and drop the three example databases:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE copy_one;
DROP DATABASE
DROP DATABASE copy_zero;
DROP DATABASE
DROP DATABASE latin_db;
DROP DATABASE
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving test objects in template1 | Every database you create afterwards gets them too. | Drop them from template1, or create the database from template0. |
Connecting to template0 | FATAL: database "template0" is not currently accepting connections | Leave template0 alone, and use TEMPLATE template0 in CREATE DATABASE instead. |
A different encoding without template0 | ERROR: 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
- PostgreSQL 19 documentation: Template Databases (opens in a new tab)
- PostgreSQL 19 documentation: CREATE DATABASE (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 4: Create a Database
- How to Create a Database in pgAdmin 4
- How to Check the Size of a Database in PostgreSQL
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