diff --git a/meta-oe/recipes-dbs/postgresql/files/pg_config b/meta-oe/recipes-dbs/postgresql/files/pg_config
new file mode 100755
index 0000000000..1fab031a90
--- /dev/null
+++ b/meta-oe/recipes-dbs/postgresql/files/pg_config
@@ -0,0 +1,43 @@
+#!/bin/sh
+# pg_config for cross builds, the real one is a target binary.
+# Build paths are answered inside the sysroot, install paths as on the target.
+
+sysroot=$(cd "$(dirname "$0")" && pwd -P)
+sysroot=${sysroot%@CROSSSCRIPTS@}
+
+value() {
+	case $1 in
+	--bindir) echo "@BINDIR@" ;;
+	--docdir) echo "@DOCDIR@" ;;
+	--htmldir) echo "@DOCDIR@" ;;
+	--includedir) echo "$sysroot@INCLUDEDIR@" ;;
+	--pkgincludedir) echo "$sysroot@PKGINCLUDEDIR@" ;;
+	--includedir-server) echo "$sysroot@PKGINCLUDEDIR@/server" ;;
+	--libdir) echo "$sysroot@LIBDIR@" ;;
+	--pkglibdir) echo "@PKGLIBDIR@" ;;
+	--localedir) echo "@LOCALEDIR@" ;;
+	--mandir) echo "@MANDIR@" ;;
+	--sharedir) echo "@SHAREDIR@" ;;
+	--sysconfdir) echo "@SYSCONFDIR@" ;;
+	--pgxs) echo "$sysroot@PKGLIBDIR@/pgxs/src/makefiles/pgxs.mk" ;;
+	--version) echo "PostgreSQL @VERSION@" ;;
+	*)
+		echo "pg_config: $1 is not available in a cross build" >&2
+		exit 1
+		;;
+	esac
+}
+
+all="bindir docdir htmldir includedir pkgincludedir includedir-server \
+	libdir pkglibdir localedir mandir sharedir sysconfdir pgxs version"
+
+if [ $# -eq 0 ]; then
+	for opt in $all; do
+		printf '%s = %s\n' "$(echo "$opt" | tr a-z A-Z)" "$(value "--$opt")"
+	done
+	exit 0
+fi
+
+for arg; do
+	value "$arg"
+done
diff --git a/meta-oe/recipes-dbs/postgresql/postgresql.inc b/meta-oe/recipes-dbs/postgresql/postgresql.inc
index 76e90df765..5ac0513085 100644
--- a/meta-oe/recipes-dbs/postgresql/postgresql.inc
+++ b/meta-oe/recipes-dbs/postgresql/postgresql.inc
@@ -30,6 +30,7 @@ SRC_URI = "https://ftp.postgresql.org/pub/source/v${PV}/${BP}.tar.bz2 \
     file://postgresql-setup \
     file://postgresql.service \
     file://run-ptest \
+    file://pg_config \
 "
 
 LEAD_SONAME = "libpq.so"
@@ -284,16 +285,65 @@ do_install:append() {
     install -m 0644 ${UNPACKDIR}/postgresql.service ${D}${systemd_unitdir}/system
     sed -i -e 's,@BINDIR@,${bindir},g' \
         ${D}${systemd_unitdir}/system/postgresql.service
-    # Remove the build path
+    # Make the installed PGXS makefiles usable from extension recipes: take
+    # the toolchain from the environment and drop the build paths. PostgreSQL
+    # 18 skips pg_config when prefix is set, the first expression undoes that.
     if [ -f ${D}${libdir}/${BPN}/pgxs/src/Makefile.global ]; then
-        sed -i -e 's#${RECIPE_SYSROOT}##g' \
-               -e 's#${RECIPE_SYSROOT_NATIVE}##g' \
-               -e 's#${WORKDIR}##g' \
-               -e 's#${TMPDIR}##g' \
-             ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        sed -i \
+            -e 's#^ifneq (,$(if $(PGXS),,1)$(prefix))$#ifndef PGXS#' \
+            -e 's#^CC = .*#CC := $(CC)#' \
+            -e 's#^CXX = .*#CXX := $(CXX)#' \
+            -e 's#^CPP = .*#CPP := $(CPP)#' \
+            -e 's#^LD = .*#LD := $(LD)#' \
+            -e 's# -fcanon-prefix-map##g' \
+            -e 's# -f\(file\|debug\|macro\)-prefix-map=[^ ]*##g' \
+            -e 's#^CFLAGS = \(.*\)#CFLAGS := \1 $(CFLAGS)#' \
+            -e 's#^CXXFLAGS = \(.*\)#CXXFLAGS := \1 $(CXXFLAGS)#' \
+            -e 's#^LDFLAGS = \(.*\)#LDFLAGS := \1 $(LDFLAGS)#' \
+            -e '/^CFLAGS :=/s# -I${STAGING_INCDIR}/${PYTHON_DIR}##' \
+            -e 's#${RECIPE_SYSROOT}${includedir}#$(includedir)#g' \
+            -e 's#${RECIPE_SYSROOT}${libdir}#$(libdir)#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/perl-native/perl#perl#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/python3-native/python3#python3#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/##g' \
+            -e 's#${TMPDIR}/hosttools/##g' \
+            -e 's#${RECIPE_SYSROOT_NATIVE}##g' \
+            -e 's#${RECIPE_SYSROOT}##g' \
+            -e 's#${WORKDIR}##g' \
+            -e 's#${TMPDIR}##g' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        # PGXS installs headers and libraries into directories pg_config
+        # reports inside the sysroot, strip the sysroot there
+        sed -i -e '/^pgxsdir = /a pg_sysroot := $(patsubst %$(pgxsdir)/src/makefiles/pgxs.mk,%,$(PGXS))' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        sed -i -e 's#$(DESTDIR)$(includedir_server)#$(DESTDIR)$(includedir_server:$(pg_sysroot)%=%)#g' \
+            ${D}${libdir}/${BPN}/pgxs/src/makefiles/pgxs.mk
+        sed -i -e 's#$(DESTDIR)$(libdir)#$(DESTDIR)$(libdir:$(pg_sysroot)%=%)#g' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.shlib
     fi
 }
 
+# pg_config is a target binary, stage a script for extension builds
+SYSROOT_PREPROCESS_FUNCS += "postgresql_sysroot_preprocess"
+postgresql_sysroot_preprocess() {
+    install -d ${SYSROOT_DESTDIR}${bindir_crossscripts}
+    sed -e 's,@CROSSSCRIPTS@,${bindir_crossscripts},g' \
+        -e 's,@BINDIR@,${bindir},g' \
+        -e 's,@DOCDIR@,${docdir}/${BPN},g' \
+        -e 's,@INCLUDEDIR@,${includedir},g' \
+        -e 's,@PKGINCLUDEDIR@,${includedir}/${BPN},g' \
+        -e 's,@LIBDIR@,${libdir},g' \
+        -e 's,@PKGLIBDIR@,${libdir}/${BPN},g' \
+        -e 's,@LOCALEDIR@,${datadir}/locale,g' \
+        -e 's,@MANDIR@,${mandir},g' \
+        -e 's,@SHAREDIR@,${datadir}/${BPN},g' \
+        -e 's,@SYSCONFDIR@,${sysconfdir}/${BPN},g' \
+        -e 's,@VERSION@,${PV},g' \
+        ${UNPACKDIR}/pg_config \
+        > ${SYSROOT_DESTDIR}${bindir_crossscripts}/pg_config
+    chmod 0755 ${SYSROOT_DESTDIR}${bindir_crossscripts}/pg_config
+}
+
 SSTATE_SCAN_FILES += "Makefile.global"
 SSTATE_SCAN_FILES:remove = "*_config"
 
