pg_upgrade from 17.10 to 18.4 on Ubuntu fails: Finding the real data directory for the target cluster sh: 1: (null)/postgres: not found
Dear Friends,
Today a pg_upgrade failed with this awkward message:
Finding the real data directory for the source cluster ok
Finding the real data directory for the target cluster sh: 1:
(null)/postgres: not found
I checked the source code of pg_upgrade.c and thus options.c and found
that the (null) above stems from an uninitialised variable called cmd,
declared in line 433 of option.c
Line 468 reads:
snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C
data_directory",
cluster->bindir, cluster->pgconfig);
Calling the postgres binary (from the package, didn't compile myself)
works fine and gives me the correct real data directory:
$ /usr/lib/postgresql/18/bin/postgres -D /etc/postgresql/18/main -C
data_directory
/var/lib/postgresql/18/main
So how come that cmd is NULL in line 468? I actually cannot find where
the variable was given any content, so it's NULL. This generates the
error message with "(null)/postgres not found"
Obviously, cmd should contain the path of the oldbin directory, which is
to be derived from the path of the pg_upgrade being called. I didn't use
the -B option because the documentation says that it's optional and will
use the directory of pg_upgrade.
https://www.postgresql.org/docs/current/pgupgrade.html tells me:
| -B| /|bindir|/
| --new-bindir=|/|bindir
|/ the new PostgreSQL executable directory; default is the
directory where pg_upgrade resides
Obviously, this doesn't work as described.
When giving the path to the new bin directory with -B, pg_upgrade works
fine.
Please correct the code so that cmd gets initialised or alter the
documentation making the option -B mandatory.
Kind Regards,
Holger
--
Holger Jakobs, Bergisch Gladbach, Germany
[ redirecting to pgsql-bugs ]
Holger Jakobs <holger@jakobs.com> writes:
Dear Friends,
Today a pg_upgrade failed with this awkward message:
Finding the real data directory for the source cluster ok
Finding the real data directory for the target cluster sh: 1:
(null)/postgres: not found
I can reproduce this if (1) I omit -B (and don't give PGBINNEW
either), and (2) the target -D directory is a config-only directory,
that is it contains postgresql.conf but not PG_VERSION. Given (1),
pg_upgrade is supposed to assume that -B should be the directory
that it itself was executed from. However, that is filled in by
setup(), which does not run till after adjust_data_dir(), which is
reaching the "Finding the real data directory" bit because of (2).
So we arrive there with cluster->bindir still NULL, and kaboom.
So the temporary workaround for Holger is to specify -B explicitly,
but somebody needs to rethink the order of initialization steps here.
Maybe just move that bit of setup() into parseCommandLine()?
It looks like this is an error of pretty long standing...
regards, tom lane
Import Notes
Reply to msg id not found: f5bb021f-8f45-4825-9b4a-39241ea27a2f@jakobs.comReference msg id not found: f5bb021f-8f45-4825-9b4a-39241ea27a2f@jakobs.com
Hi,
On Wed, Aug 12, 2026 at 12:50:57PM +0200, Holger Jakobs wrote:
Today a pg_upgrade failed with this awkward message:
Finding the real data directory for the source cluster ok
Finding the real data directory for the target cluster sh: 1:
(null)/postgres: not found
What was the exact command line you used?
Obviously, cmd should contain the path of the oldbin directory, which is to
be derived from the path of the pg_upgrade being called.
Are you saying you ran the 17 pg_update binary? I am not sure that is
supported/supposed to work, one should use the pg_upgrade binary from
the newer Postgres version (unless I misunderstood you).
Still, the error isn't great and should be improved / this case should
be caught earlier in pre-flight checks probably.
Michael