Instiq
Chapter 2 · Operations management·v1.0.0·Updated 7/12/2026·~10 min

What's changed: Initial version (topic S2)

2.1Installation and the Database Cluster

Key points

Learn initdb, the very first step in getting PostgreSQL running, and the concept and structure of the database cluster it creates. We also cover the two template databases automatically generated at cluster creation, template0 and template1, and how their roles differ.

Installing PostgreSQL alone does not give you any databases yet. The very first step is creating the storage area that holds the entire set of data the server manages—initializing the database cluster. Understanding this foundation and the mechanisms built into it automatically underlies everything else in operations.

2.1.1initdb and the database cluster

  • initdb is the command that creates a new database cluster. It writes configuration files and the initial set of system catalogs under the directory you specify (via the -D option or the PGDATA environment variable). Re-running it against a directory that already holds a cluster results in an error.
  • A database cluster is the entire collection of databases managed by a single initdb (everything served by one PostgreSQL server process). On disk it exists as a single directory tree (the data directory), inside which multiple individual databases coexist. You can create and drop many databases within one cluster, but the cluster itself is a single physical area.

2.1.2Template databases

  • template0 is a frozen, pristine template that must never be modified. It is referenced only in special cases, such as creating a new database with a different encoding or locale from the cluster default. It is not normally something you connect to or customize.
  • template1 is the default blueprint used when creating a new database. Unless told otherwise, createdb or CREATE DATABASE copies template1 to build the new database. A common operational technique is to pre-install extensions or settings you want shared by every database into template1, so they automatically appear in every database created afterward.
Exam point

The staples: initdb creates a new database cluster; the cluster is the entire set of databases managed by one server; template0 is frozen and must not be modified, while template1 is the default blueprint that CREATE DATABASE copies. Swapping the roles of template0 and template1 is a classic wrong-answer distractor.

Walking through a typical initial setup cements the concept. First, initialize the cluster by specifying a data directory, e.g. initdb -D /var/lib/pgsql/data. At this point you can specify the authentication method (the initial contents of pg_hba.conf) and the encoding/locale via options such as --auth, --encoding, and --locale, which become cluster-wide defaults affecting every database created afterward. Once initdb finishes, the cluster automatically contains an administrative database named postgres, plus the two template databases template0 and template1. To create a new application database, you would run something like createdb myapp, which internally copies template1 to produce a database named myapp. If a requirement calls for "one special database with a different encoding than the cluster default," using the normal template1 would carry over existing settings, so instead you explicitly specify the frozen template0 (createdb -T template0 --encoding=...) to build it from scratch—this distinction is a common practical exam point.

ItemRoleModifiable?
initdbCreate a new cluster(the creation action itself)
template0Frozen initial templateNo (referenced only for special createdb cases)
template1Default blueprint (copied by CREATE DATABASE)Yes (e.g. pre-install shared extensions)
Warning

Trap: "a new database is created by copying template0" is wrong—by default it copies template1; template0 is the frozen template you must not modify. Also, "initdb adds a new database to an existing cluster" is wrong—initdb creates the cluster itself; adding a database to an existing cluster is the job of createdb or CREATE DATABASE.

Diagram of cluster initialization by initdb and the template0/template1 relationship.
initdb -> cluster -> createdb (cloned from a template)

2.1.3Section summary

  • initdb creates a new database cluster. One cluster = the entire database cluster (set of databases) managed by one server
  • template0 = frozen, never modify (referenced only for special encodings etc.); template1 = the default blueprint (copied by createdb/CREATE DATABASE; also used to pre-install shared extensions)

Sign in to track progress — Log in.

Quick check

(just a quick review)

Q1. Right after installing PostgreSQL on a new server, no database cluster exists yet. Which command creates a new cluster?

Q2. When you create a new database with createdb or CREATE DATABASE without specifying otherwise, which template database provides the basis for its contents?

Q3. You want to create one special database with a different encoding than the cluster default. Using template1 as-is would carry over its existing settings, which is a problem. What is the correct approach?

Check your understandingPractice questions for Chapter 2: Operations management