Sub topic 6
Initializing the New Cluster
Assume you can identify the old cluster's data directory, PostgreSQL binary directories, locale, encoding, and data-checksum setting. You should also know which operating-system user owns the database files. The new cluster must be initialized before pg_upgrade can check or migrate it.
Why a new cluster is required
A PostgreSQL major release can change internal data formats, system catalogs, and storage behavior. For this reason, PostgreSQL 17 cannot safely treat a PostgreSQL 16 data directory as its own. The new server needs a fresh directory created by the PostgreSQL 17 initdb program.
initdb creates a cluster. A cluster is a data directory containing the system catalogs, configuration defaults, transaction metadata, and initial databases such as postgres and template1.
Do not copy the old data directory and do not run the new server against it. The new directory must be separate and must be owned by the database operating-system user.
Match the important settings
The new cluster should use settings compatible with the old cluster. At minimum, compare these values before running initdb:
| Setting | Why it matters | New cluster requirement |
|---|---|---|
| Encoding | Defines how text is stored | Match the old cluster |
| Locale | Controls sorting and locale-sensitive behavior | Match the old cluster |
| Data checksums | Detects damaged data pages | Use the same checksum choice |
| Tablespace paths | Locate data outside the main directory | Prepare compatible empty paths |
You can inspect the old cluster while it is running with SHOW server_encoding;, SHOW lc_collate;, and SHOW lc_ctype;. Check whether data checksums are enabled with pg_controldata, using the old version's binary directory.
The locale must exist on the operating system where the new cluster is created. A locale name that worked on the old host might be absent on the new host. Use locale -a to list available locales. If the requested locale is unavailable, stop and fix the operating-system locale installation rather than choosing a different locale casually. Changing collation behavior can change index ordering and query results.
Run the new version's initdb
Run the initdb from the new PostgreSQL installation. The command below uses placeholder paths and assumes that the old cluster uses UTF-8 and the en_US.UTF-8 locale:
/opt/pg17/bin/initdb \
-D /data/pg17 \
--encoding=UTF8 \
--locale=en_US.UTF-8Run this command as the database owner, commonly postgres, or use the account and permissions required by your installation. The target directory should not contain an existing PostgreSQL cluster. If the old cluster uses data checksums, add --data-checksums when initializing the new cluster:
/opt/pg17/bin/initdb \
-D /data/pg17 \
--encoding=UTF8 \
--locale=en_US.UTF-8 \
--data-checksumsThe command writes a new PG_VERSION file and creates the catalog structure expected by PostgreSQL 17. It does not copy application tables, roles, or extensions from the old cluster. pg_upgrade handles the supported migration of those objects later.
Configure the new cluster for the check
After initdb, review the generated postgresql.conf and pg_hba.conf. Do not replace them with the old files as a whole. Configuration parameters can be added, removed, or renamed between major versions.
The new cluster must remain stopped while pg_upgrade --check runs. It normally does not need to accept client connections for this step. Give it a separate data directory and, if you start it for testing, use a different port from the old cluster.
Preserve a record of the old cluster's settings before changing anything. Later, copy only relevant settings into the new configuration after checking that each parameter is supported by the new PostgreSQL version.
Exercise
Why must the new cluster use the new PostgreSQL version's initdb instead of a copied old data directory?
Show answer
The new version needs its own system catalogs and internal metadata. initdb creates those structures, while a copied old directory may contain formats the new server cannot safely interpret.