Migrating postgresql database to postgresql 18.0-1 from postgresql 17
Synopsis
i got this error when i'm trying to start my postgresql service after arch linux recent update:
[yzuaf@archworkplus awesomeGo]$ sudo systemctl start postgresql
[sudo] password for yzuaf:
Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xeu postgresql.service" for details.
and when i see the log message:
[yzuaf@archworkplus awesomeGo]$ journalctl -xeu postgresql.service
░░ The unit postgresql.service has entered the 'failed' state with result 'exit-code'.
Nov 10 08:47:45 archworkplus systemd[1]: Failed to start PostgreSQL database server.
░░ Subject: A start job for unit postgresql.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit postgresql.service has finished with a failure.
░░
░░ The job identifier is 5372 and the job result is failed.
[yzuaf@archworkplus awesomeGo]$ sudo systemctl status postgresql.service
× postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; ::disabled; preset: ::disabled)
Active: failed (Result: exit-code) since Mon 2025-11-10 08:47:45 WIB; 54s ago
Invocation: c667c4c18b944857b196158f3e48007d
Docs: man:postgres(1)
Process: 25821 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=1/FAILURE)
Mem peak: 2.4M
CPU: 57ms
Nov 10 08:47:45 archworkplus systemd[1]: Starting PostgreSQL database server...
Nov 10 08:47:45 archworkplus postgres[25821]: An old version of the database format was found.
Nov 10 08:47:45 archworkplus postgres[25821]: See https://wiki.archlinux.org/index.php/PostgreSQL#Upgrading_PostgreSQL
Nov 10 08:47:45 archworkplus systemd[1]: postgresql.service: Control process exited, code=exited, status=1/FAILURE
Nov 10 08:47:45 archworkplus systemd[1]: ::::postgresql.service: Failed with result 'exit-code'.
Nov 10 08:47:45 archworkplus systemd[1]: Failed to start PostgreSQL database server.
and i discovered that the errors come from my postgresql version is not compatible with the current database, i use postgresql 18.0-1 while my database is still using version 17:
[yzuaf@archworkplus awesomeGo]$ sudo cat /var/lib/postgres/data/PG_VERSION
17
Solution
so i'm gonna migrate the database in order to use version 18. i'm gonna create a backup in case it crashed:
sudo cp -a /var/lib/postgres/data /var/lib/postgres/data_backup_17
and now the fun part begin, first we login into the postgres user:
sudo -iu postgres
now we can initialize new database cluster using version 18:
[postgres@archworkplus ~]$ initdb -D /var/lib/postgres/data18
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are enabled.
creating directory /var/lib/postgres/data18 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... Asia/Jakarta
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgres/data18 -l logfile start
[postgres@archworkplus ~]$ exit
logout
now the main part, we can use [^1]pg_upgrade:
[yzuaf@archworkplus awesomeGo]$ sudo -iu postgres /usr/bin/pg_upgrade \
-b /opt/pgsql-17/bin \
-B /usr/bin \
-d /var/lib/postgres/data \
-D /var/lib/postgres/data18 \
-U postgres
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
Checking database connection settings ok
Checking database user is the install user ok
Checking for prepared transactions ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for valid logical replication slots ok
Checking for subscription state ok
Checking data type usage ok
Checking for objects affected by Unicode update ok
Checking for not-null constraint inconsistencies ok
Creating dump of global objects ok
Creating dump of database schemas
ok
Checking for presence of required libraries ok
Checking database user is the install user ok
Checking for prepared transactions ok
Checking for new cluster tablespace directories ok
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
------------------
Setting locale and encoding for new cluster ok
Analyzing all rows in the new cluster ok
Freezing all rows in the new cluster ok
Deleting files from new pg_xact ok
Copying old pg_xact to new server ok
Setting oldest XID for new cluster ok
Setting next transaction ID and epoch for new cluster ok
Deleting files from new pg_multixact/offsets ok
Copying old pg_multixact/offsets to new server ok
Deleting files from new pg_multixact/members ok
Copying old pg_multixact/members to new server ok
Setting next multixact ID and offset for new cluster ok
Resetting WAL archives ok
Setting frozenxid and minmxid counters in new cluster ok
Restoring global objects in the new cluster ok
Restoring database schemas in the new cluster
ok
Copying user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to delete old cluster ok
Checking for extension updates ok
Upgrade Complete
----------------
Some statistics are not transferred by pg_upgrade.
Once you start the new server, consider running these two commands:
/usr/bin/vacuumdb -U postgres --all --analyze-in-stages --missing-stats-only
/usr/bin/vacuumdb -U postgres --all --analyze-only
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
now, let's move the new cluster to main data:
sudo mv /var/lib/postgres/data18 /var/lib/postgres/data
and you can restart the postgres service:
[yzuaf@archworkplus awesomeGo]$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; ::disabled; preset: ::disabled)
Active: active (running) since Mon 2025-11-10 09:03:28 WIB; 4s ago
Invocation: a02d37f842e54489946725e8e0536715
Docs: man:postgres(1)
Process: 29010 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGROOT}/data (code=exited, status=0/SUCCESS)
Main PID: 29013 (postgres)
Tasks: 9 (limit: 18127)
Memory: 22.2M (peak: 23.2M)
CPU: 164ms
CGroup: /system.slice/postgresql.service
├─29013 /usr/bin/postgres -D /var/lib/postgres/data
├─29016 "postgres: io worker 0"
├─29017 "postgres: io worker 1"
├─29018 "postgres: io worker 2"
├─29019 "postgres: checkpointer "
├─29020 "postgres: background writer "
├─29022 "postgres: walwriter "
├─29023 "postgres: autovacuum launcher "
└─29024 "postgres: logical replication launcher "
Nov 10 09:03:28 archworkplus systemd[1]: Starting PostgreSQL database server...
Nov 10 09:03:28 archworkplus postgres[29013]: 2025-11-10 09:03:28.813 WIB [29013] LOG: starting PostgreSQL 18.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 15.2.1 20250813, 64-bit
Nov 10 09:03:28 archworkplus postgres[29013]: 2025-11-10 09:03:28.816 WIB [29013] LOG: listening on IPv6 address "::1", port 5432
Nov 10 09:03:28 archworkplus postgres[29013]: 2025-11-10 09:03:28.816 WIB [29013] LOG: listening on IPv4 address "127.0.0.1", port 5432
Nov 10 09:03:28 archworkplus postgres[29013]: 2025-11-10 09:03:28.817 WIB [29013] LOG: listening on Unix socket "/run/postgresql/.s.PGSQL.5432"
Nov 10 09:03:28 archworkplus postgres[29021]: 2025-11-10 09:03:28.825 WIB [29021] LOG: database system was shut down at 2025-11-10 08:59:24 WIB
Nov 10 09:03:28 archworkplus postgres[29013]: 2025-11-10 09:03:28.831 WIB [29013] LOG: database system is ready to accept connections
Nov 10 09:03:28 archworkplus systemd[1]: Started PostgreSQL database server.
voilahh!!