Sub topic 6
Initializing the new cluster
This chapter assumes you can run pg_ctl and initdb, know what a data directory is, and have both the old and new PostgreSQL binaries installed side by side. By the end you will be able to create an empty new cluster that pg_upgrade accepts, and you will know which settings have to match the old cluster and which do not.
Why the new cluster must exist first
pg_upgrade does not create the target cluster. It expects an existing, empty cluster that was created by the new version's initdb. It starts the old and new servers temporarily, dumps the schema from the old one, restores that schema into the new one, and then moves or links the data files.
Because the schema is restored into the new cluster, the new cluster must be a place where those objects can live. The files that pg_upgrade carries over are tied to the old cluster's physical settings. If those settings differ, the check fails or the data could be misread. Initialization is therefore the step where you copy the old cluster's physical settings onto the new one.
What must match
Some settings are fixed when the cluster is created and cannot be changed afterwards. pg_upgrade compares these between the clusters and refuses to continue if they differ.
| Setting | Where to read it on the old cluster | initdb option |
|---|---|---|
| Encoding of the template databases | `pg_database.encoding` | `--encoding` |
| `LC_COLLATE` and `LC_CTYPE` | `pg_database.datcollate`, `datctype` | `--locale`, `--lc-collate`, `--lc-ctype` |
| Locale provider (PostgreSQL 15 and later) | `pg_database.datlocprovider` | `--locale-provider`, `--icu-locale` |
| Data checksums | `SHOW data_checksums;` or `pg_controldata` | `--data-checksums` or `--no-data-checksums` |
| Bootstrap superuser name | `\du` or `pg_authid` | `--username` |
Settings that are not checked still deserve attention. These include lc_messages, lc_monetary, lc_numeric and lc_time. They affect output formatting and message language, not stored data, so a difference will not stop the upgrade. It can still surprise applications that parse messages or format numbers.
Reading the old cluster's settings
Start the old cluster as normal and ask it directly. This query lists the settings that fix how text is stored and sorted for every database.
SELECT datname,
pg_encoding_to_char(encoding) AS encoding,
datcollate,
datctype,
datlocprovider
FROM pg_database
ORDER BY datname;datlocprovider exists in PostgreSQL 15 and later. On older clusters, remove that column. The value c means the libc provider, i means ICU, and b means the built-in provider (PostgreSQL 17 and later).
Then check checksums and the superuser:
SHOW data_checksums;
SELECT rolname FROM pg_roles WHERE oid = 10;The role with OID 10 is always the bootstrap superuser, the role created by initdb. On many systems it is postgres, but packagers and cloud migrations sometimes use another name. The new cluster must use the same name.
If the old cluster is already stopped, pg_controldata reads the same information from the control file without starting the server:
/opt/pg16/bin/pg_controldata /data/pg16 | grep -E "checksum|WAL segment|Database cluster state"Running initdb
Run initdb from the new version's bin directory, as the same operating system user that owns the old data directory. Using the old cluster's values, the command from this tutorial looks like this:
/opt/pg17/bin/initdb -D /data/pg17 --encoding=UTF8 --locale=en_US.UTF-8The paths, encoding and locale are placeholders. Replace them with the values you read from the old cluster.
Several details matter here.
- Use the new version's
initdb. Running the old one creates a cluster of the old version, andpg_upgradewill reject it. - Use a new, empty directory.
initdbrefuses a non-empty directory. Do not point it at the old data directory. - Keep file ownership and permissions the same. The directory should be owned by the service user and have mode
0700. If the old cluster uses group access (mode0750), initialize the new one with--allow-group-accessso the two clusters behave alike for backup tooling. - Match the superuser. If the old bootstrap superuser is not
postgres, add--username=olduser.
A fuller command that sets every checked option explicitly looks like this:
/opt/pg17/bin/initdb \
-D /data/pg17 \
--username=postgres \
--encoding=UTF8 \
--locale=en_US.UTF-8 \
--data-checksumsSpelling out each option is better than relying on defaults. Defaults depend on the environment variables of whoever runs the command, and they change between versions.
Locale providers
If the old cluster uses ICU collations as its default (PostgreSQL 15 and later), the new cluster must too:
/opt/pg17/bin/initdb \
-D /data/pg17 \
--encoding=UTF8 \
--locale-provider=icu \
--icu-locale=en-US \
--locale=en_US.UTF-8You still need --locale for the libc settings, because those apply to parts of the system that do not use ICU. Match the datlocprovider value of the template databases, and for ICU also the locale name stored in the old cluster (daticulocale in PostgreSQL 15 and 16, datlocale in 17).
Data checksums
Data checksums detect corrupted pages when they are read. pg_upgrade requires both clusters to agree on whether they are on. If the old cluster has checksums, pass --data-checksums. If it does not, you must make sure the new cluster does not have them either.
Keep a checksum policy decision separate from the upgrade. Changing the checksum setting at the same time as a major upgrade adds a second risk to the maintenance window. Many teams upgrade first with matching settings, then enable checksums afterwards in a separate window.
WAL segment size and other creation-time options
The WAL segment size (default 16 MB) is also chosen at initdb time through --wal-segsize. pg_upgrade does not require the two clusters to use the same value. You should still set it deliberately. If your old cluster used a larger size for high write volume, repeat that choice so archiving and replication tooling see the same behavior.
A good habit is to keep the full initdb command in your upgrade runbook, along with the output of pg_controldata from the old cluster, so another person can reproduce it.
Comparing the two clusters
Once initdb finishes, compare the control files. Neither cluster has to be running.
for v in 16 17; do
echo "== PostgreSQL $v =="
/opt/pg$v/bin/pg_controldata /data/pg$v \
| grep -E "Data page checksum version|Bytes per WAL segment|Database block size"
doneExpected output has the same checksum version (0 for off, 1 for on) and the same block size in both. The block size (default 8192 bytes) is fixed at compile time, not at initdb. If you use a package from a different vendor or a custom build with a different block size, the clusters cannot be upgraded with pg_upgrade, and you need dump and restore or logical replication.
Configuration files are not carried over
initdb writes a default postgresql.conf, pg_hba.conf and pg_ident.conf into the new data directory. pg_upgrade does not copy your old versions. Review them by hand before the upgrade, because the check and the upgrade itself start the new server using this configuration.
Things to review:
shared_preload_libraries. If the old cluster preloads extensions such aspg_stat_statements, add them in the new file, and make sure the new version's shared library is installed. A missing library stops the temporary new server from starting.- Memory and connection settings. Copy
shared_buffers,max_connections,work_memand similar. Parameters can be renamed or removed between versions, so read the release notes rather than pasting the whole old file. max_connectionsand related capacity. For a standby, settings such asmax_connectionsmust be at least as high on the standby as on the primary. Carry them over now to avoid trouble later.port. If the old cluster listens on 5432, the new one will try the same port.pg_upgradepicks its own temporary ports (50432 by default), so there is no conflict during the upgrade. After the upgrade you decide which cluster uses the production port.pg_hba.conf.pg_upgradeconnects as the superuser many times through a Unix socket in the current directory. If your rules require passwords, it will ask for the password repeatedly. Put a~/.pgpassentry in place or temporarily allowpeerortrustfor local connections on both clusters, then restore the strict rules after the upgrade.
Instead of copying whole files, consider an include approach. Put your site settings in a separate file and add include = 'site.conf' at the end of the new postgresql.conf. Then you can review a short file rather than a generated one.
Leave the new cluster stopped and empty
Do not start the new cluster after initdb, and do not create databases, roles or extensions in it. pg_upgrade needs the new cluster to contain only the template databases and the default postgres database. Extra roles or objects can cause errors such as a database or role that already exists. If you accidentally started it and made changes, delete the directory and run initdb again. That costs seconds.
Common mistakes at this step
- Running
initdbwith a different locale because the shell environment changed. Always pass--localeand--encodingexplicitly. - Using the new cluster's default
postgressuperuser name when the old one is different. - Leaving checksums on by default in PostgreSQL 18 or later while the old cluster has them off.
- Forgetting
shared_preload_libraries, so the new server cannot start during the check. - Initializing the new directory on a different filesystem than the old one when you plan to use
--link. Hard links cannot cross filesystems, so keep both directories on the same mount. The next chapters cover this choice in detail. - Copying an operating system image to a host with a newer glibc and keeping the same libc locale name. Collation order can change, which silently invalidates text indexes. Plan to run
REINDEXon indexes that use affected collations after the upgrade.
Exercise
Your old PostgreSQL 16 cluster was created with initdb --encoding=UTF8 --locale=de_DE.UTF-8, has data checksums off, and its bootstrap superuser is called dbadmin. Write the initdb command for a new PostgreSQL 18 cluster in /data/pg18 that pg_upgrade --check will accept, and say why each non-default option is there.
Show answer
/opt/pg18/bin/initdb -D /data/pg18 --username=dbadmin --encoding=UTF8 --locale=deDE.UTF-8 --no-data-checksums. The --username option makes the new bootstrap superuser match the old one. --encoding and --locale match the template databases' encoding, `LCCOLLATE and LCCTYPE`, which pgupgrade compares. --no-data-checksums is needed because PostgreSQL 18 turns checksums on by default and the old cluster has them off, and pg_upgrade requires the two settings to agree.
Exercise
The cluster is initialized and stopped. Name two files in the new data directory that you must review by hand before running the check, and give one thing that can go wrong if you do not.
Show answer
postgresql.conf and pghba.conf (pgident.conf is also acceptable). If sharedpreloadlibraries is not carried over, or names a library not installed for the new version, the temporary new server may fail to start or behave differently. If pghba.conf requires passwords, pgupgrade prompts for them repeatedly, which can stall an unattended run.