diff --git a/pkgs/applications/blockchains/lighthouse/default.nix b/pkgs/applications/blockchains/lighthouse/default.nix
index 2afce895da6e5..619354ce5dc81 100644
--- a/pkgs/applications/blockchains/lighthouse/default.nix
+++ b/pkgs/applications/blockchains/lighthouse/default.nix
@@ -8,7 +8,7 @@
 , nodePackages
 , openssl
 , pkg-config
-, postgresql
+, libpq
 , protobuf
 , rustPlatform
 , rust-jemalloc-sys
@@ -132,7 +132,7 @@ rustPlatform.buildRustPackage rec {
 
   nativeCheckInputs = [
     nodePackages.ganache
-    postgresql
+    libpq
   ];
 
   passthru = {
diff --git a/pkgs/applications/editors/rstudio/default.nix b/pkgs/applications/editors/rstudio/default.nix
index ff0c468fc509d..e1787064b6f7b 100644
--- a/pkgs/applications/editors/rstudio/default.nix
+++ b/pkgs/applications/editors/rstudio/default.nix
@@ -26,7 +26,7 @@
 , llvmPackages
 , yaml-cpp
 , soci
-, postgresql
+, libpq
 , nodejs
 , qmake
 , server ? false # build server version
@@ -98,7 +98,7 @@ in
       libuuid
       yaml-cpp
       soci
-      postgresql
+      libpq
       quarto
     ] ++ (if server then [
       sqlite.dev
diff --git a/pkgs/applications/gis/grass/default.nix b/pkgs/applications/gis/grass/default.nix
index 68bbcb46b5260..6ff9a77ee9a23 100644
--- a/pkgs/applications/gis/grass/default.nix
+++ b/pkgs/applications/gis/grass/default.nix
@@ -27,7 +27,7 @@
 , netcdf
 , pdal
 , pkg-config
-, postgresql
+, libpq
 , proj
 , python311Packages
 , readline
@@ -79,7 +79,7 @@ stdenv.mkDerivation (finalAttrs: {
     (libxml2.override { enableHttp = true; })
     netcdf
     pdal
-    postgresql
+    libpq
     proj
     readline
     sqlite
@@ -106,7 +106,7 @@ stdenv.mkDerivation (finalAttrs: {
     "--with-openmp"
     "--with-pdal"
     "--with-postgres"
-    "--with-postgres-libs=${postgresql.lib}/lib/"
+    "--with-postgres-libs=${libpq}/lib/"
     "--with-proj-includes=${proj.dev}/include"
     "--with-proj-libs=${proj}/lib"
     "--with-proj-share=${proj}/share/proj"
diff --git a/pkgs/applications/gis/qgis/unwrapped-ltr.nix b/pkgs/applications/gis/qgis/unwrapped-ltr.nix
index bc807b7e10749..40594c70348d7 100644
--- a/pkgs/applications/gis/qgis/unwrapped-ltr.nix
+++ b/pkgs/applications/gis/qgis/unwrapped-ltr.nix
@@ -27,7 +27,7 @@
 , ninja
 , openssl
 , pdal
-, postgresql
+, libpq
 , proj
 , protobuf
 , python311
@@ -118,7 +118,7 @@ in mkDerivation rec {
     netcdf
     openssl
     pdal
-    postgresql
+    libpq
     proj
     protobuf
     qca-qt5
diff --git a/pkgs/applications/gis/qgis/unwrapped.nix b/pkgs/applications/gis/qgis/unwrapped.nix
index 577dfdf5e17f4..9b3f6708c352c 100644
--- a/pkgs/applications/gis/qgis/unwrapped.nix
+++ b/pkgs/applications/gis/qgis/unwrapped.nix
@@ -27,7 +27,7 @@
 , ninja
 , openssl
 , pdal
-, postgresql
+, libpq
 , proj
 , protobuf
 , python311
@@ -119,7 +119,7 @@ in mkDerivation rec {
     netcdf
     openssl
     pdal
-    postgresql
+    libpq
     proj
     protobuf
     qca-qt5
diff --git a/pkgs/applications/gis/spatialite-gui/default.nix b/pkgs/applications/gis/spatialite-gui/default.nix
index a8bdedabaa374..fed95138da1ab 100644
--- a/pkgs/applications/gis/spatialite-gui/default.nix
+++ b/pkgs/applications/gis/spatialite-gui/default.nix
@@ -15,7 +15,7 @@
 , lz4
 , minizip
 , openjpeg
-, postgresql
+, libpq
 , proj
 , sqlite
 , virtualpg
@@ -52,7 +52,7 @@ stdenv.mkDerivation rec {
     lz4
     minizip
     openjpeg
-    postgresql
+    libpq
     proj
     sqlite
     virtualpg
diff --git a/pkgs/applications/misc/pgmodeler/default.nix b/pkgs/applications/misc/pgmodeler/default.nix
index 59c732ec81d06..6e8886548e2e0 100644
--- a/pkgs/applications/misc/pgmodeler/default.nix
+++ b/pkgs/applications/misc/pgmodeler/default.nix
@@ -8,7 +8,7 @@
 , qmake
 , qtwayland
 , qtsvg
-, postgresql
+, libpq
 , cups
 , libxml2
 }:
@@ -26,15 +26,14 @@ stdenv.mkDerivation rec {
 
   nativeBuildInputs = [ pkg-config qmake wrapQtAppsHook copyDesktopItems ];
   qmakeFlags = [ "pgmodeler.pro" "CONFIG+=release" ] ++ lib.optionals stdenv.hostPlatform.isDarwin [
-    "PGSQL_INC=${lib.getDev postgresql}/include"
-    "PGSQL_LIB=${lib.getLib postgresql}/lib/libpq.dylib"
+    "PGSQL_INC=${lib.getDev libpq}/include"
+    "PGSQL_LIB=${lib.getLib libpq}/lib/libpq.dylib"
     "XML_INC=${libxml2.dev}/include/libxml2"
     "XML_LIB=${libxml2.out}/lib/libxml2.dylib"
     "PREFIX=${placeholder "out"}/Applications/pgModeler.app/Contents"
   ];
 
-  # todo: libpq would suffice here. Unfortunately this won't work, if one uses only postgresql.lib here.
-  buildInputs = [ postgresql qtsvg ]
+  buildInputs = [ libpq qtsvg ]
     ++ lib.optionals stdenv.hostPlatform.isLinux [ qtwayland ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ cups libxml2 ];
 
diff --git a/pkgs/applications/networking/instant-messengers/teamspeak/server.nix b/pkgs/applications/networking/instant-messengers/teamspeak/server.nix
index d1be4c251fc70..08333fe997885 100644
--- a/pkgs/applications/networking/instant-messengers/teamspeak/server.nix
+++ b/pkgs/applications/networking/instant-messengers/teamspeak/server.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, postgresql, autoPatchelfHook, writeScript }:
+{ lib, stdenv, fetchurl, libpq, autoPatchelfHook, writeScript }:
 
 let
   arch = if stdenv.hostPlatform.is64bit then "amd64" else "x86";
@@ -13,7 +13,7 @@ in stdenv.mkDerivation rec {
       else "sha256-aMEDOnvBeKfzG8lDFhU8I5DYgG53IsCDBMV2MUyJi2g=";
   };
 
-  buildInputs = [ stdenv.cc.cc postgresql.lib ];
+  buildInputs = [ stdenv.cc.cc libpq ];
 
   nativeBuildInputs = [ autoPatchelfHook ];
 
diff --git a/pkgs/applications/networking/p2p/gnunet/default.nix b/pkgs/applications/networking/p2p/gnunet/default.nix
index 048ba5a354961..9798ea5e7edae 100644
--- a/pkgs/applications/networking/p2p/gnunet/default.nix
+++ b/pkgs/applications/networking/p2p/gnunet/default.nix
@@ -3,7 +3,7 @@
 , makeWrapper, ncurses, pkg-config, libxml2, sqlite, zlib
 , libpulseaudio, libopus, libogg, jansson, libsodium
 
-, postgresqlSupport ? true, postgresql }:
+, postgresqlSupport ? true, libpq }:
 
 stdenv.mkDerivation rec {
   pname = "gnunet";
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
     adns curl gmp gnutls libextractor libgcrypt libgnurl libidn
     libmicrohttpd libunistring libxml2 ncurses gettext libsodium
     sqlite zlib libpulseaudio libopus libogg jansson
-  ] ++ lib.optional postgresqlSupport postgresql;
+  ] ++ lib.optional postgresqlSupport libpq;
 
   preConfigure = ''
     # Brute force: since nix-worker chroots don't provide
diff --git a/pkgs/applications/office/kexi/default.nix b/pkgs/applications/office/kexi/default.nix
index 132194d0c7824..20310462e18f7 100644
--- a/pkgs/applications/office/kexi/default.nix
+++ b/pkgs/applications/office/kexi/default.nix
@@ -4,7 +4,7 @@
   breeze-icons, karchive, kcodecs, kcompletion, kconfig, kconfigwidgets, kcoreaddons,
   kcrash, kguiaddons, ki18n, kiconthemes, kitemviews, kio, ktexteditor, ktextwidgets,
   kwidgetsaddons, kxmlgui,
-  kdb, kproperty, kreport, lcms2, libmysqlclient, marble, postgresql
+  kdb, kproperty, kreport, lcms2, libmysqlclient, marble, libpq
 }:
 
 mkDerivation rec {
@@ -23,7 +23,7 @@ mkDerivation rec {
     breeze-icons karchive kcodecs kcompletion kconfig kconfigwidgets kcoreaddons
     kcrash kguiaddons ki18n kiconthemes kitemviews kio ktexteditor ktextwidgets
     kwidgetsaddons kxmlgui
-    kdb kproperty kreport lcms2 libmysqlclient marble postgresql
+    kdb kproperty kreport lcms2 libmysqlclient marble libpq
   ];
 
   propagatedUserEnvPkgs = [ kproperty ];
diff --git a/pkgs/applications/office/ledger-web/default.nix b/pkgs/applications/office/ledger-web/default.nix
index 00e3636fc3971..59d0c6afd3384 100644
--- a/pkgs/applications/office/ledger-web/default.nix
+++ b/pkgs/applications/office/ledger-web/default.nix
@@ -1,5 +1,5 @@
 { lib, bundlerApp, bundlerUpdateScript
-, withPostgresql ? true, postgresql
+, withPostgresql ? true, libpq
 , withSqlite ? false, sqlite
 }:
 
@@ -8,7 +8,7 @@ bundlerApp {
   gemdir = ./.;
   exes = [ "ledger_web" ];
 
-  buildInputs =    lib.optional withPostgresql postgresql
+  buildInputs =    lib.optional withPostgresql libpq
                 ++ lib.optional withSqlite sqlite;
 
   passthru.updateScript = bundlerUpdateScript "ledger-web";
diff --git a/pkgs/applications/office/libreoffice/default.nix b/pkgs/applications/office/libreoffice/default.nix
index efd3b01a7d169..18888c09e27ad 100644
--- a/pkgs/applications/office/libreoffice/default.nix
+++ b/pkgs/applications/office/libreoffice/default.nix
@@ -113,7 +113,7 @@
 , kwindowsystem ? null
 , variant ? "fresh"
 , symlinkJoin
-, postgresql
+, libpq
 , makeFontsConf
 , amiri
 , caladea
@@ -395,7 +395,7 @@ in stdenv.mkDerivation (finalAttrs: {
     pam
     perl
     poppler
-    postgresql
+    libpq
     python311
     sane-backends
     unixODBC
diff --git a/pkgs/build-support/rust/default-crate-overrides.nix b/pkgs/build-support/rust/default-crate-overrides.nix
index 407f7e652c381..cb6d74c8b6975 100644
--- a/pkgs/build-support/rust/default-crate-overrides.nix
+++ b/pkgs/build-support/rust/default-crate-overrides.nix
@@ -34,7 +34,7 @@
 , openssl
 , pango
 , pkg-config
-, postgresql
+, libpq
 , protobuf
 , python3
 , rdkafka
@@ -259,7 +259,7 @@ in
 
   pq-sys = attr: {
     nativeBuildInputs = [ pkg-config ];
-    buildInputs = [ postgresql ];
+    buildInputs = [ libpq ];
   };
 
   prost-build = attr: {
diff --git a/pkgs/by-name/al/algol68g/package.nix b/pkgs/by-name/al/algol68g/package.nix
index 1622952b3ce56..52dc0aa5ea6f2 100644
--- a/pkgs/by-name/al/algol68g/package.nix
+++ b/pkgs/by-name/al/algol68g/package.nix
@@ -7,7 +7,7 @@
 , mpfr
 , ncurses
 , plotutils
-, postgresql
+, libpq
 , pkg-config
 , withPDFDoc ? true
 }:
@@ -34,7 +34,7 @@ stdenv.mkDerivation (finalAttrs: {
     gmp
     gsl
     plotutils
-    postgresql
+    libpq
   ];
 
   strictDeps = true;
diff --git a/pkgs/by-name/cp/cppdb/package.nix b/pkgs/by-name/cp/cppdb/package.nix
index b71a6c0730310..20efd65205c7f 100644
--- a/pkgs/by-name/cp/cppdb/package.nix
+++ b/pkgs/by-name/cp/cppdb/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, cmake, sqlite, libmysqlclient, postgresql, unixODBC }:
+{ lib, stdenv, fetchurl, cmake, sqlite, libmysqlclient, libpq, unixODBC }:
 
 stdenv.mkDerivation rec {
   pname = "cppdb";
@@ -10,7 +10,7 @@ stdenv.mkDerivation rec {
   };
 
   nativeBuildInputs = [ cmake ];
-  buildInputs = [ sqlite libmysqlclient postgresql unixODBC ];
+  buildInputs = [ sqlite libmysqlclient libpq unixODBC ];
 
   cmakeFlags = [ "--no-warn-unused-cli" ];
   env.NIX_CFLAGS_COMPILE = "-I${libmysqlclient}/include/mysql -L${libmysqlclient}/lib/mysql";
diff --git a/pkgs/by-name/cy/cyrus-imapd/package.nix b/pkgs/by-name/cy/cyrus-imapd/package.nix
index 1bba318c5531d..5e28732536125 100644
--- a/pkgs/by-name/cy/cyrus-imapd/package.nix
+++ b/pkgs/by-name/cy/cyrus-imapd/package.nix
@@ -34,7 +34,7 @@
   openssl,
   pcre2,
   perl,
-  postgresql,
+  libpq,
   rsync,
   shapelib,
   sqlite,
@@ -115,7 +115,7 @@ stdenv.mkDerivation (finalAttrs: {
       xapian
     ]
     ++ lib.optionals withMySQL [ libmysqlclient ]
-    ++ lib.optionals withPgSQL [ postgresql ]
+    ++ lib.optionals withPgSQL [ libpq ]
     ++ lib.optionals withSQLite [ sqlite ];
 
   enableParallelBuilding = true;
diff --git a/pkgs/by-name/di/diesel-cli/package.nix b/pkgs/by-name/di/diesel-cli/package.nix
index b4f721c5305db..1d6cb2f8c2d96 100644
--- a/pkgs/by-name/di/diesel-cli/package.nix
+++ b/pkgs/by-name/di/diesel-cli/package.nix
@@ -10,7 +10,7 @@
   nix-update-script,
   openssl,
   pkg-config,
-  postgresql,
+  libpq,
   sqlite,
   testers,
   zlib,
@@ -47,7 +47,7 @@ rustPlatform.buildRustPackage rec {
     ++ lib.optional stdenv.hostPlatform.isDarwin darwin.apple_sdk.frameworks.Security
     ++ lib.optional (stdenv.hostPlatform.isDarwin && mysqlSupport) libiconv
     ++ lib.optional sqliteSupport sqlite
-    ++ lib.optional postgresqlSupport postgresql
+    ++ lib.optional postgresqlSupport libpq
     ++ lib.optionals mysqlSupport [
       libmysqlclient
       zlib
diff --git a/pkgs/by-name/dr/drogon/package.nix b/pkgs/by-name/dr/drogon/package.nix
index eeda204d6175c..73ab490e2a092 100644
--- a/pkgs/by-name/dr/drogon/package.nix
+++ b/pkgs/by-name/dr/drogon/package.nix
@@ -3,7 +3,7 @@
 , openssl, brotli, c-ares
 # optional databases
 , sqliteSupport ? true, sqlite
-, postgresSupport ? false, postgresql
+, postgresSupport ? false, libpq
 , redisSupport ? false, hiredis
 , mysqlSupport ? false, libmysqlclient, mariadb }:
 
@@ -34,7 +34,7 @@ stdenv.mkDerivation (finalAttrs: {
     brotli
     c-ares
   ] ++ lib.optional sqliteSupport sqlite
-    ++ lib.optional postgresSupport postgresql
+    ++ lib.optional postgresSupport libpq
     ++ lib.optional redisSupport hiredis
     # drogon uses mariadb for mysql (see https://github.com/drogonframework/drogon/wiki/ENG-02-Installation#Library-Dependencies)
     ++ lib.optionals mysqlSupport [ libmysqlclient mariadb ];
diff --git a/pkgs/by-name/ds/dspam/package.nix b/pkgs/by-name/ds/dspam/package.nix
index f32316428a693..6d600e65f6e39 100644
--- a/pkgs/by-name/ds/dspam/package.nix
+++ b/pkgs/by-name/ds/dspam/package.nix
@@ -2,7 +2,7 @@
 , gawk, gnused, gnugrep, coreutils, which
 , perlPackages
 , withMySQL ? false, zlib, mariadb-connector-c
-, withPgSQL ? false, postgresql
+, withPgSQL ? false, libpq
 , withSQLite ? false, sqlite
 , withDB ? false, db
 }:
@@ -32,7 +32,7 @@ in stdenv.mkDerivation rec {
 
   buildInputs = [ perlPackages.perl ]
                 ++ lib.optionals withMySQL [ zlib mariadb-connector-c.out ]
-                ++ lib.optional withPgSQL postgresql
+                ++ lib.optional withPgSQL libpq
                 ++ lib.optional withSQLite sqlite
                 ++ lib.optional withDB db;
   nativeBuildInputs = [ makeWrapper ];
@@ -62,7 +62,7 @@ in stdenv.mkDerivation rec {
     "--with-mysql-includes=${mariadb-connector-c.dev}/include/mysql"
     "--with-mysql-libraries=${mariadb-connector-c.out}/lib/mysql"
   ]
-    ++ lib.optional withPgSQL "--with-pgsql-libraries=${postgresql.lib}/lib";
+    ++ lib.optional withPgSQL "--with-pgsql-libraries=${libpq}/lib";
 
   # Workaround build failure on -fno-common toolchains like upstream
   # gcc-10. Otherwise build fails as:
diff --git a/pkgs/by-name/ex/exim/package.nix b/pkgs/by-name/ex/exim/package.nix
index bf593354da92f..9c1fad89fcbb9 100644
--- a/pkgs/by-name/ex/exim/package.nix
+++ b/pkgs/by-name/ex/exim/package.nix
@@ -3,7 +3,7 @@
 , procps, killall
 , enableLDAP ? false, openldap
 , enableMySQL ? false, libmysqlclient, zlib
-, enablePgSQL ? false, postgresql
+, enablePgSQL ? false, libpq
 , enableSqlite ? false, sqlite
 , enableAuthDovecot ? false, dovecot
 , enablePAM ? false, pam
@@ -30,7 +30,7 @@ in stdenv.mkDerivation rec {
   buildInputs = [ coreutils db openssl perl' pcre2 libxcrypt ]
     ++ lib.optional enableLDAP openldap
     ++ lib.optionals enableMySQL [ libmysqlclient zlib ]
-    ++ lib.optional enablePgSQL postgresql
+    ++ lib.optional enablePgSQL libpq
     ++ lib.optionals enableSqlite [ sqlite sqlite.dev zlib ]
     ++ lib.optional enableAuthDovecot dovecot
     ++ lib.optional enablePAM pam
@@ -80,8 +80,8 @@ in stdenv.mkDerivation rec {
       ''}
       ${lib.optionalString enablePgSQL ''
         s:^# \(LOOKUP_PGSQL=yes\)$:\1:
-        s:^\(LOOKUP_LIBS\)=\(.*\):\1=\2 -lpq -L${postgresql.lib}/lib:
-        s:^# \(LOOKUP_LIBS\)=.*:\1=-lpq -L${postgresql.lib}/lib:
+        s:^\(LOOKUP_LIBS\)=\(.*\):\1=\2 -lpq -L${libpq}/lib:
+        s:^# \(LOOKUP_LIBS\)=.*:\1=-lpq -L${libpq}/lib:
       ''}
       ${lib.optionalString enableSqlite ''
         s:^# \(LOOKUP_SQLITE=yes\)$:\1:
diff --git a/pkgs/by-name/fl/fluent-bit/package.nix b/pkgs/by-name/fl/fluent-bit/package.nix
index 8c6c762e66c34..cb9ace135572b 100644
--- a/pkgs/by-name/fl/fluent-bit/package.nix
+++ b/pkgs/by-name/fl/fluent-bit/package.nix
@@ -6,7 +6,7 @@
   flex,
   bison,
   systemd,
-  postgresql,
+  libpq,
   openssl,
   libyaml,
   darwin,
@@ -36,7 +36,7 @@ stdenv.mkDerivation (finalAttrs: {
     [
       openssl
       libyaml
-      postgresql
+      libpq
     ]
     ++ lib.optionals stdenv.hostPlatform.isLinux [ systemd ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [
diff --git a/pkgs/by-name/fr/freeradius/package.nix b/pkgs/by-name/fr/freeradius/package.nix
index c846408690216..e43c5681c8fcd 100644
--- a/pkgs/by-name/fr/freeradius/package.nix
+++ b/pkgs/by-name/fr/freeradius/package.nix
@@ -12,7 +12,7 @@
 , withLdap ? true, openldap
 , withMemcached ? false, libmemcached
 , withMysql ? false, libmysqlclient
-, withPostgresql ? false, postgresql
+, withPostgresql ? false, libpq
 , withPcap ? true, libpcap
 , withRedis ? false, hiredis
 , withRest ? false, curl
@@ -42,7 +42,7 @@ stdenv.mkDerivation rec {
     ++ lib.optional withLdap openldap
     ++ lib.optional withMemcached libmemcached
     ++ lib.optional withMysql libmysqlclient
-    ++ lib.optional withPostgresql postgresql
+    ++ lib.optional withPostgresql libpq
     ++ lib.optional withPcap libpcap
     ++ lib.optional withRedis hiredis
     ++ lib.optional withRest curl
diff --git a/pkgs/by-name/ga/gammu/package.nix b/pkgs/by-name/ga/gammu/package.nix
index 78d5e010213cf..14f7bd1b4c20c 100644
--- a/pkgs/by-name/ga/gammu/package.nix
+++ b/pkgs/by-name/ga/gammu/package.nix
@@ -1,7 +1,7 @@
 { lib, stdenv, fetchFromGitHub, substituteAll, pkg-config, cmake, bluez, libusb1, curl
 , libiconv, gettext, sqlite, bash, dialog
 , dbiSupport ? false, libdbi ? null, libdbiDrivers ? null
-, postgresSupport ? false, postgresql ? null
+, postgresSupport ? false, libpq ? null
 }:
 
 stdenv.mkDerivation rec {
@@ -30,7 +30,7 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ bash bluez libusb1 curl gettext sqlite libiconv ]
   ++ lib.optionals dbiSupport [ libdbi libdbiDrivers ]
-  ++ lib.optionals postgresSupport [ postgresql ];
+  ++ lib.optionals postgresSupport [ libpq ];
 
   meta = with lib; {
     homepage = "https://wammu.eu/gammu/";
diff --git a/pkgs/by-name/in/inspircd/package.nix b/pkgs/by-name/in/inspircd/package.nix
index dd821ce119591..b249e5fb20563 100644
--- a/pkgs/by-name/in/inspircd/package.nix
+++ b/pkgs/by-name/in/inspircd/package.nix
@@ -67,7 +67,7 @@ in
 , pkg-config
 , libargon2
 , openldap
-, postgresql
+, libpq
 , libmysqlclient
 , pcre
 , pcre2
@@ -97,7 +97,7 @@ let
     ];
     ldap            = [ openldap ];
     mysql           = [ libmysqlclient ];
-    pgsql           = [ postgresql ];
+    pgsql           = [ libpq ];
     regex_pcre      = [ pcre ];
     regex_pcre2     = [ pcre2 ];
     regex_re2       = [ re2 ];
diff --git a/pkgs/by-name/ke/kepler/package.nix b/pkgs/by-name/ke/kepler/package.nix
index 35e601dac163c..4e4639f3ad3fd 100644
--- a/pkgs/by-name/ke/kepler/package.nix
+++ b/pkgs/by-name/ke/kepler/package.nix
@@ -4,7 +4,7 @@
 , fetchFromGitHub
 , openssl
 , pkg-config
-, postgresql
+, libpq
 , rustPlatform
 , zstd
 }:
@@ -28,7 +28,7 @@ rustPlatform.buildRustPackage rec {
 
   buildInputs = [
     openssl
-    postgresql
+    libpq
     zstd
   ] ++ lib.optionals stdenv.hostPlatform.isDarwin [
     darwin.apple_sdk.frameworks.Security
diff --git a/pkgs/by-name/li/libdbiDrivers/package.nix b/pkgs/by-name/li/libdbiDrivers/package.nix
index 6537e3ae2a3e8..1d9d086f137c0 100644
--- a/pkgs/by-name/li/libdbiDrivers/package.nix
+++ b/pkgs/by-name/li/libdbiDrivers/package.nix
@@ -2,7 +2,7 @@
 # TODO: migrate away from overriding packages to null
 , libmysqlclient ? null
 , sqlite ? null
-, postgresql ? null
+, libpq ? null
 }:
 
 stdenv.mkDerivation rec {
@@ -14,7 +14,7 @@ stdenv.mkDerivation rec {
     sha256 = "0m680h8cc4428xin4p733azysamzgzcmv4psjvraykrsaz6ymlj3";
   };
 
-  buildInputs = [ libdbi sqlite postgresql ] ++ lib.optional (libmysqlclient != null) libmysqlclient;
+  buildInputs = [ libdbi sqlite ] ++ lib.optional (libmysqlclient != null) libmysqlclient;
 
   patches = [
     # https://sourceforge.net/p/libdbi-drivers/libdbi-drivers/ci/24f48b86c8988ee3aaebc5f303d71e9d789f77b6
@@ -40,8 +40,10 @@ stdenv.mkDerivation rec {
     "--with-sqlite3"
     "--with-sqlite3-incdir=${sqlite.dev}/include/sqlite"
     "--with-sqlite3-libdir=${sqlite.out}/lib/sqlite"
-  ] ++ lib.optionals (postgresql != null) [
+  ] ++ lib.optionals (libpq != null) [
     "--with-pgsql"
+    "--with-pgsql-incdir=${libpq.dev}/include"
+    "--with-pgsql-libdir=${libpq}/lib/"
   ];
 
   env.NIX_CFLAGS_COMPILE = toString (lib.optionals stdenv.cc.isClang [
diff --git a/pkgs/by-name/li/libpqxx/package.nix b/pkgs/by-name/li/libpqxx/package.nix
index d2ed5b35c617c..67c845585c38f 100644
--- a/pkgs/by-name/li/libpqxx/package.nix
+++ b/pkgs/by-name/li/libpqxx/package.nix
@@ -2,7 +2,7 @@
   lib,
   stdenv,
   fetchFromGitHub,
-  postgresql,
+  libpq,
   python3,
 }:
 
@@ -23,12 +23,11 @@ stdenv.mkDerivation (finalAttrs: {
   ];
 
   nativeBuildInputs = [
-    postgresql.dev
     python3
   ];
 
   buildInputs = [
-    postgresql.lib
+    libpq
   ];
 
   postPatch = ''
diff --git a/pkgs/by-name/ma/mapserver/package.nix b/pkgs/by-name/ma/mapserver/package.nix
index 43996b4c92d58..a434fa1b96545 100644
--- a/pkgs/by-name/ma/mapserver/package.nix
+++ b/pkgs/by-name/ma/mapserver/package.nix
@@ -20,7 +20,7 @@
   librsvg,
   libxml2,
   pkg-config,
-  postgresql,
+  libpq,
   proj,
   protobufc,
   python3,
@@ -63,7 +63,7 @@ stdenv.mkDerivation rec {
     libpng
     librsvg
     (libxml2.override { enableHttp = true; })
-    postgresql
+    libpq
     proj
     protobufc
     zlib
diff --git a/pkgs/by-name/n8/n8n/package.nix b/pkgs/by-name/n8/n8n/package.nix
index 29dfa2a7f7f63..65ec8f292c42d 100644
--- a/pkgs/by-name/n8/n8n/package.nix
+++ b/pkgs/by-name/n8/n8n/package.nix
@@ -11,7 +11,7 @@
   xcbuild,
   libkrb5,
   libmongocrypt,
-  postgresql,
+  libpq,
   makeWrapper,
   nix-update-script,
 }:
@@ -44,7 +44,7 @@ stdenv.mkDerivation (finalAttrs: {
     nodejs
     libkrb5
     libmongocrypt
-    postgresql
+    libpq
   ];
 
   buildPhase = ''
diff --git a/pkgs/by-name/od/odyssey/package.nix b/pkgs/by-name/od/odyssey/package.nix
index 726b12182ebb9..a7cee037bb85c 100644
--- a/pkgs/by-name/od/odyssey/package.nix
+++ b/pkgs/by-name/od/odyssey/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, cmake, openssl, postgresql, zstd, fetchpatch }:
+{ lib, stdenv, fetchFromGitHub, cmake, openssl, libpq, zstd, fetchpatch }:
 
 stdenv.mkDerivation rec {
   pname = "odyssey";
@@ -20,8 +20,13 @@ stdenv.mkDerivation rec {
   ];
 
   nativeBuildInputs = [ cmake ];
-  buildInputs = [ openssl postgresql zstd ];
-  cmakeFlags = [ "-DPQ_LIBRARY=${postgresql.lib}/lib" "-DBUILD_COMPRESSION=ON" ];
+  buildInputs = [ openssl libpq zstd ];
+  cmakeFlags = [
+    "-DBUILD_COMPRESSION=ON"
+    "-DPOSTGRESQL_INCLUDE_DIR=${lib.getDev libpq}/include/postgresql/server"
+    "-DPOSTGRESQL_LIBRARY=${libpq}/lib"
+    "-DPOSTGRESQL_LIBPGPORT=${lib.getDev libpq}/lib"
+  ];
 
   installPhase = ''
     install -Dm755 -t $out/bin sources/odyssey
diff --git a/pkgs/by-name/op/opendbx/package.nix b/pkgs/by-name/op/opendbx/package.nix
index ba66ba5be18a9..6c3cd7b945806 100644
--- a/pkgs/by-name/op/opendbx/package.nix
+++ b/pkgs/by-name/op/opendbx/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, readline, libmysqlclient, postgresql, sqlite }:
+{ lib, stdenv, fetchurl, readline, libmysqlclient, libpq, sqlite }:
 
 let inherit (lib) getDev; in
 
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
     configureFlagsArray=(--with-backends="mysql pgsql sqlite3")
   '';
 
-  buildInputs = [ readline libmysqlclient postgresql sqlite ];
+  buildInputs = [ readline libmysqlclient libpq sqlite ];
 
   env.NIX_CFLAGS_COMPILE = toString [
     # Needed with GCC 12
diff --git a/pkgs/by-name/os/osm2pgsql/package.nix b/pkgs/by-name/os/osm2pgsql/package.nix
index 85bd3dcac2e7c..1cd8dd2011e44 100644
--- a/pkgs/by-name/os/osm2pgsql/package.nix
+++ b/pkgs/by-name/os/osm2pgsql/package.nix
@@ -8,7 +8,7 @@
 , cli11
 , zlib
 , boost
-, postgresql
+, libpq
 , python3
 , withLuaJIT ? false
 , lua
@@ -48,7 +48,7 @@ stdenv.mkDerivation (finalAttrs: {
     libosmium
     nlohmann_json
     opencv
-    postgresql
+    libpq
     potrace
     proj
     protozero
diff --git a/pkgs/by-name/pa/pam_pgsql/package.nix b/pkgs/by-name/pa/pam_pgsql/package.nix
index 2eabcefe584c6..c9cee98050fc4 100644
--- a/pkgs/by-name/pa/pam_pgsql/package.nix
+++ b/pkgs/by-name/pa/pam_pgsql/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, autoreconfHook, pkg-config, postgresql, libgcrypt, pam, libxcrypt }:
+{ lib, stdenv, fetchFromGitHub, autoreconfHook, pkg-config, libpq, libgcrypt, pam, libxcrypt }:
 
 stdenv.mkDerivation rec {
   pname = "pam_pgsql";
@@ -12,7 +12,7 @@ stdenv.mkDerivation rec {
   };
 
   nativeBuildInputs = [ autoreconfHook pkg-config ];
-  buildInputs = [ libgcrypt pam postgresql libxcrypt ];
+  buildInputs = [ libgcrypt pam libpq libxcrypt ];
 
   meta = with lib; {
     description = "Support to authenticate against PostgreSQL for PAM-enabled appliations";
diff --git a/pkgs/by-name/pd/pdal/package.nix b/pkgs/by-name/pd/pdal/package.nix
index 2b43f15bef2ae..523f6fb0eb275 100644
--- a/pkgs/by-name/pd/pdal/package.nix
+++ b/pkgs/by-name/pd/pdal/package.nix
@@ -17,7 +17,7 @@
 , libxml2
 , openscenegraph
 , pkg-config
-, postgresql
+, libpq
 , proj
 , sqlite
 , tiledb
@@ -51,7 +51,7 @@ stdenv.mkDerivation (finalAttrs: {
     libtiff
     (libxml2.override { enableHttp = true; })
     openscenegraph
-    postgresql
+    libpq
     proj
     sqlite
     tiledb
diff --git a/pkgs/by-name/pd/pdns/package.nix b/pkgs/by-name/pd/pdns/package.nix
index 87b4f65eb51bc..664d50f606571 100644
--- a/pkgs/by-name/pd/pdns/package.nix
+++ b/pkgs/by-name/pd/pdns/package.nix
@@ -12,7 +12,7 @@
   openssl,
   systemd,
   mariadb-connector-c,
-  postgresql,
+  libpq,
   lua,
   openldap,
   geoip,
@@ -37,7 +37,7 @@ stdenv.mkDerivation (finalAttrs: {
   buildInputs = [
     boost
     mariadb-connector-c
-    postgresql
+    libpq
     lua
     openldap
     sqlite
diff --git a/pkgs/by-name/pg/pgbackrest/package.nix b/pkgs/by-name/pg/pgbackrest/package.nix
index 78fa48fde463b..9dc179dea2109 100644
--- a/pkgs/by-name/pg/pgbackrest/package.nix
+++ b/pkgs/by-name/pg/pgbackrest/package.nix
@@ -8,7 +8,7 @@
 , libbacktrace
 , bzip2
 , lz4
-, postgresql
+, libpq
 , libxml2
 , libyaml
 , zlib
@@ -40,7 +40,7 @@ stdenv.mkDerivation rec {
     libbacktrace
     bzip2
     lz4
-    postgresql
+    libpq
     libxml2
     libyaml
     zlib
diff --git a/pkgs/by-name/pg/pgmanage/package.nix b/pkgs/by-name/pg/pgmanage/package.nix
index 0c435dc04fa41..94fcf3e2971c1 100644
--- a/pkgs/by-name/pg/pgmanage/package.nix
+++ b/pkgs/by-name/pg/pgmanage/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, postgresql, openssl, nixosTests } :
+{ lib, stdenv, fetchFromGitHub, libpq, openssl, nixosTests } :
 stdenv.mkDerivation rec {
   pname = "pgmanage";
   # The last release 11.0.1 from 2018 fails the NixOS test
@@ -21,7 +21,7 @@ stdenv.mkDerivation rec {
     ./configure --prefix $out
   '';
 
-  buildInputs = [ postgresql openssl ];
+  buildInputs = [ libpq openssl ];
 
   passthru.tests.sign-in = nixosTests.pgmanage;
 
diff --git a/pkgs/by-name/pg/pgpool/package.nix b/pkgs/by-name/pg/pgpool/package.nix
index 21105ea723ca5..5498ae7031a90 100644
--- a/pkgs/by-name/pg/pgpool/package.nix
+++ b/pkgs/by-name/pg/pgpool/package.nix
@@ -1,7 +1,7 @@
 { lib
 , stdenv
 , fetchurl
-, postgresql
+, libpq
 , openssl
 , libxcrypt
 , withPam ? stdenv.hostPlatform.isLinux
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
   };
 
   buildInputs = [
-    postgresql
+    libpq
     openssl
     libxcrypt
   ] ++ lib.optional withPam pam;
diff --git a/pkgs/by-name/pm/pmacct/package.nix b/pkgs/by-name/pm/pmacct/package.nix
index 3cd43ec9a873c..12364105d644f 100644
--- a/pkgs/by-name/pm/pmacct/package.nix
+++ b/pkgs/by-name/pm/pmacct/package.nix
@@ -10,7 +10,7 @@
 , withJansson ? true, jansson
 , withNflog ? true, libnetfilter_log
 , withSQLite ? true, sqlite
-, withPgSQL ? true, postgresql
+, withPgSQL ? true, libpq
 , withMysql ? true, libmysqlclient, zlib, numactl
 , gnutlsSupport ? false, gnutls
 , testers
@@ -38,7 +38,7 @@ stdenv.mkDerivation (finalAttrs: {
   ] ++ lib.optional withJansson jansson
   ++ lib.optional withNflog libnetfilter_log
   ++ lib.optional withSQLite sqlite
-  ++ lib.optional withPgSQL postgresql
+  ++ lib.optional withPgSQL libpq
   ++ lib.optionals withMysql [ libmysqlclient zlib numactl ]
   ++ lib.optional gnutlsSupport gnutls;
 
diff --git a/pkgs/by-name/ps/pspg/package.nix b/pkgs/by-name/ps/pspg/package.nix
index 4424b8cc47d9b..e9fe6840a53f3 100644
--- a/pkgs/by-name/ps/pspg/package.nix
+++ b/pkgs/by-name/ps/pspg/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, gnugrep, ncurses, pkg-config, installShellFiles, readline, postgresql }:
+{ lib, stdenv, fetchFromGitHub, gnugrep, ncurses, pkg-config, installShellFiles, readline, libpq }:
 
 stdenv.mkDerivation rec {
   pname = "pspg";
@@ -12,7 +12,7 @@ stdenv.mkDerivation rec {
   };
 
   nativeBuildInputs = [ pkg-config installShellFiles ];
-  buildInputs = [ gnugrep ncurses readline postgresql ];
+  buildInputs = [ gnugrep ncurses readline libpq ];
 
   makeFlags = [ "PREFIX=${placeholder "out"}" ];
 
diff --git a/pkgs/by-name/ps/psqlodbc/package.nix b/pkgs/by-name/ps/psqlodbc/package.nix
index 38f5c47eef1b2..115b688f2b66e 100644
--- a/pkgs/by-name/ps/psqlodbc/package.nix
+++ b/pkgs/by-name/ps/psqlodbc/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, postgresql, openssl, autoreconfHook, nix-update-script
+{ lib, stdenv, fetchFromGitHub, libpq, openssl, autoreconfHook, nix-update-script
 , withLibiodbc ? false, libiodbc
 , withUnixODBC ? true, unixODBC
 }:
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
   };
 
   buildInputs = [
-    postgresql
+    libpq
     openssl
   ]
   ++ lib.optional withLibiodbc libiodbc
@@ -35,7 +35,7 @@ stdenv.mkDerivation rec {
   };
 
   configureFlags = [
-    "--with-libpq=${lib.getDev postgresql}/bin/pg_config"
+    "--with-libpq=${lib.getDev libpq}"
   ]
   ++ lib.optional withLibiodbc "--with-iodbc=${libiodbc}";
 
@@ -44,6 +44,6 @@ stdenv.mkDerivation rec {
     description = "ODBC driver for PostgreSQL";
     license = licenses.lgpl2;
     platforms = platforms.unix;
-    maintainers = postgresql.meta.maintainers;
+    maintainers = libpq.meta.maintainers;
   };
 }
diff --git a/pkgs/by-name/sa/saga/package.nix b/pkgs/by-name/sa/saga/package.nix
index e7642e0425157..eaa9e027376da 100644
--- a/pkgs/by-name/sa/saga/package.nix
+++ b/pkgs/by-name/sa/saga/package.nix
@@ -19,7 +19,7 @@
   opencv,
   vigra,
   pdal,
-  postgresql,
+  libpq,
   darwin,
   unixODBC,
   poppler,
@@ -63,7 +63,7 @@ stdenv.mkDerivation rec {
       libharu
       opencv
       vigra
-      postgresql
+      libpq
       libiodbc
       xz
       qhull
diff --git a/pkgs/by-name/so/sope/package.nix b/pkgs/by-name/so/sope/package.nix
index 89372fca3f639..d8b44a7969d6d 100644
--- a/pkgs/by-name/so/sope/package.nix
+++ b/pkgs/by-name/so/sope/package.nix
@@ -1,5 +1,5 @@
 { gnustep, lib, fetchFromGitHub, fetchpatch, libxml2, openssl
-, openldap, mariadb, libmysqlclient, postgresql }:
+, openldap, mariadb, libmysqlclient, libpq }:
 
 gnustep.stdenv.mkDerivation rec {
   pname = "sope";
@@ -24,7 +24,7 @@ gnustep.stdenv.mkDerivation rec {
   buildInputs = [ gnustep.base libxml2 openssl ]
     ++ lib.optional (openldap != null) openldap
     ++ lib.optionals (mariadb != null) [ libmysqlclient mariadb ]
-    ++ lib.optional (postgresql != null) postgresql;
+    ++ lib.optional (libpq != null) libpq;
 
   # Configure directories where files are installed to. Everything is automatically
   # put into $out (thanks GNUstep) apart from the makefiles location which is where
@@ -41,7 +41,7 @@ gnustep.stdenv.mkDerivation rec {
   configureFlags = [ "--prefix=" "--disable-debug" "--enable-xml" "--with-ssl=ssl" ]
     ++ lib.optional (openldap != null) "--enable-openldap"
     ++ lib.optional (mariadb != null) "--enable-mysql"
-    ++ lib.optional (postgresql != null) "--enable-postgresql";
+    ++ lib.optional (libpq != null) "--enable-postgresql";
 
   env = {
     GNUSTEP_CONFIG_FILE = "/build/GNUstep.conf";
diff --git a/pkgs/by-name/st/stellar-core/package.nix b/pkgs/by-name/st/stellar-core/package.nix
index 16865f7709f34..ab58e85d21723 100644
--- a/pkgs/by-name/st/stellar-core/package.nix
+++ b/pkgs/by-name/st/stellar-core/package.nix
@@ -9,7 +9,7 @@
 , libtool
 , libunwind
 , pkg-config
-, postgresql
+, libpq
 , ripgrep
 , stdenv
 }:
@@ -59,7 +59,7 @@ stdenv.mkDerivation (finalAttrs: {
   propagatedBuildInputs = [
     bison
     flex
-    postgresql
+    libpq
   ];
 
   enableParallelBuilding = true;
diff --git a/pkgs/by-name/sy/syndicate_utils/package.nix b/pkgs/by-name/sy/syndicate_utils/package.nix
index ee425f44d5d78..5ac8c4e48473e 100644
--- a/pkgs/by-name/sy/syndicate_utils/package.nix
+++ b/pkgs/by-name/sy/syndicate_utils/package.nix
@@ -5,7 +5,7 @@
   libxml2,
   libxslt,
   openssl,
-  postgresql,
+  libpq,
   sqlite,
 }:
 
@@ -22,7 +22,7 @@ buildNimPackage (finalAttrs: {
   };
 
   buildInputs = [
-    postgresql.out
+    libpq
     sqlite
     libxml2
     libxslt
diff --git a/pkgs/by-name/ta/taler-challenger/package.nix b/pkgs/by-name/ta/taler-challenger/package.nix
index 0a3404e94f583..4ccc9988e0ec5 100644
--- a/pkgs/by-name/ta/taler-challenger/package.nix
+++ b/pkgs/by-name/ta/taler-challenger/package.nix
@@ -13,7 +13,7 @@
   libmicrohttpd,
   libsodium,
   libtool,
-  postgresql,
+  libpq,
   taler-exchange,
   taler-merchant,
   runtimeShell,
@@ -61,7 +61,7 @@ stdenv.mkDerivation (finalAttrs: {
     libmicrohttpd
     libsodium
     libtool
-    postgresql
+    libpq
     taler-exchange
     taler-merchant
   ];
diff --git a/pkgs/by-name/ta/taler-exchange/package.nix b/pkgs/by-name/ta/taler-exchange/package.nix
index 10dea51455a1e..b89b363594561 100644
--- a/pkgs/by-name/ta/taler-exchange/package.nix
+++ b/pkgs/by-name/ta/taler-exchange/package.nix
@@ -10,7 +10,7 @@
   libsodium,
   libunistring,
   pkg-config,
-  postgresql,
+  libpq,
   autoreconfHook,
   python3,
   recutils,
@@ -43,7 +43,7 @@ stdenv.mkDerivation (finalAttrs: {
     libmicrohttpd
     jansson
     libsodium
-    postgresql
+    libpq
     curl
     recutils
     gettext
diff --git a/pkgs/by-name/ta/taler-sync/package.nix b/pkgs/by-name/ta/taler-sync/package.nix
index 08f872abe8034..ce072d16f99a4 100644
--- a/pkgs/by-name/ta/taler-sync/package.nix
+++ b/pkgs/by-name/ta/taler-sync/package.nix
@@ -11,7 +11,7 @@
   libmicrohttpd,
   libsodium,
   libtool,
-  postgresql,
+  libpq,
   taler-exchange,
   taler-merchant,
   runtimeShell,
@@ -43,7 +43,7 @@ stdenv.mkDerivation (finalAttrs: {
     libmicrohttpd
     libsodium
     libtool
-    postgresql
+    libpq
     taler-exchange
     taler-merchant
   ];
diff --git a/pkgs/by-name/th/thc-hydra/package.nix b/pkgs/by-name/th/thc-hydra/package.nix
index 412691d2b9403..5aced688ec298 100644
--- a/pkgs/by-name/th/thc-hydra/package.nix
+++ b/pkgs/by-name/th/thc-hydra/package.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchFromGitHub, zlib, openssl, ncurses, libidn, pcre, libssh, libmysqlclient, postgresql, samba
+{ stdenv, lib, fetchFromGitHub, zlib, openssl, ncurses, libidn, pcre, libssh, libmysqlclient, libpq, samba
 , withGUI ? false, makeWrapper, pkg-config, gtk2 }:
 
 stdenv.mkDerivation rec {
@@ -28,7 +28,7 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = lib.optionals withGUI [ pkg-config makeWrapper ];
 
   buildInputs = [
-    zlib openssl ncurses libidn pcre libssh libmysqlclient postgresql
+    zlib openssl ncurses libidn pcre libssh libmysqlclient libpq
     samba
   ] ++ lib.optional withGUI gtk2;
 
diff --git a/pkgs/by-name/tn/tntdb/package.nix b/pkgs/by-name/tn/tntdb/package.nix
index 9080893d5e441..4312780af11a1 100644
--- a/pkgs/by-name/tn/tntdb/package.nix
+++ b/pkgs/by-name/tn/tntdb/package.nix
@@ -3,7 +3,7 @@
 , fetchFromGitHub
 , autoreconfHook
 , cxxtools
-, postgresql
+, libpq
 , libmysqlclient
 , sqlite
 , zlib
@@ -27,7 +27,7 @@ stdenv.mkDerivation rec {
 
   buildInputs = [
     cxxtools
-    postgresql
+    libpq
     libmysqlclient
     sqlite
     zlib
diff --git a/pkgs/by-name/ul/ulogd/package.nix b/pkgs/by-name/ul/ulogd/package.nix
index 291876cc92d24..f684e75c46317 100644
--- a/pkgs/by-name/ul/ulogd/package.nix
+++ b/pkgs/by-name/ul/ulogd/package.nix
@@ -1,6 +1,6 @@
 { stdenv, lib, fetchurl, gnumake, libnetfilter_acct, libnetfilter_conntrack
 , libnetfilter_log, libmnl, libnfnetlink, automake, autoconf, autogen, libtool
-, postgresql, libmysqlclient, sqlite
+, libpq, libmysqlclient, sqlite
 , pkg-config, libpcap, linuxdoc-tools, autoreconfHook, nixosTests }:
 
 stdenv.mkDerivation rec {
@@ -38,7 +38,7 @@ stdenv.mkDerivation rec {
     libmnl
     libnfnetlink
     libpcap
-    postgresql
+    libpq
     libmysqlclient
     sqlite
   ];
diff --git a/pkgs/by-name/vi/virtualpg/package.nix b/pkgs/by-name/vi/virtualpg/package.nix
index e15961437002d..18a4edb24c506 100644
--- a/pkgs/by-name/vi/virtualpg/package.nix
+++ b/pkgs/by-name/vi/virtualpg/package.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, validatePkgConfig, postgresql, sqlite, darwin }:
+{ lib, stdenv, fetchurl, validatePkgConfig, libpq, sqlite, darwin }:
 
 stdenv.mkDerivation rec {
   pname = "virtualpg";
@@ -11,10 +11,10 @@ stdenv.mkDerivation rec {
 
   nativeBuildInputs = [
     validatePkgConfig
-    postgresql  # for pg_config
+    libpq
   ];
 
-  buildInputs = [ postgresql sqlite ]
+  buildInputs = [ libpq sqlite ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.apple_sdk.frameworks.Kerberos ];
 
   meta = with lib; {
diff --git a/pkgs/by-name/za/zammad/package.nix b/pkgs/by-name/za/zammad/package.nix
index bf22b4b631cdc..be8d8b1b6b48f 100644
--- a/pkgs/by-name/za/zammad/package.nix
+++ b/pkgs/by-name/za/zammad/package.nix
@@ -10,6 +10,7 @@
   procps,
   ruby,
   postgresql,
+  libpq,
   imlib2,
   jq,
   moreutils,
@@ -59,7 +60,7 @@ let
     ];
     gemConfig = defaultGemConfig // {
       pg = attrs: {
-        buildFlags = [ "--with-pg-config=${lib.getDev postgresql}/bin/pg_config" ];
+        buildInputs = [ libpq ];
       };
       rszr = attrs: {
         buildInputs = [
diff --git a/pkgs/development/ada-modules/gnatcoll/db.nix b/pkgs/development/ada-modules/gnatcoll/db.nix
index 017a51e2fb750..b4e882c78d12a 100644
--- a/pkgs/development/ada-modules/gnatcoll/db.nix
+++ b/pkgs/development/ada-modules/gnatcoll/db.nix
@@ -14,7 +14,7 @@
 , gnatcoll-iconv
 , gnatcoll-readline
 , sqlite
-, postgresql
+, libpq
 }:
 
 let
@@ -29,7 +29,7 @@ let
     ];
     postgres = [
       gnatcoll-sql
-      postgresql
+      libpq
     ];
     sqlite = [
       gnatcoll-sql
diff --git a/pkgs/development/compilers/urweb/default.nix b/pkgs/development/compilers/urweb/default.nix
index f96cce8c53f1b..b8892d29e2e3a 100644
--- a/pkgs/development/compilers/urweb/default.nix
+++ b/pkgs/development/compilers/urweb/default.nix
@@ -1,5 +1,5 @@
 { lib, stdenv, fetchurl, file, openssl, mlton
-, libmysqlclient, postgresql, sqlite, gcc, icu
+, libmysqlclient, libpq, sqlite, gcc, icu
 }:
 
 stdenv.mkDerivation rec {
@@ -11,7 +11,7 @@ stdenv.mkDerivation rec {
     sha256 = "0qh6wcxfk5kf735i5gqwnkdirnnmqhnnpkfz96gz144dgz2i0c5c";
   };
 
-  buildInputs = [ openssl mlton libmysqlclient postgresql sqlite icu ];
+  buildInputs = [ openssl mlton libmysqlclient libpq sqlite icu ];
 
   prePatch = ''
     sed -e 's@/usr/bin/file@${file}/bin/file@g' -i configure
@@ -28,7 +28,7 @@ stdenv.mkDerivation rec {
     export CCARGS="-I$out/include \
                    -L${lib.getLib openssl}/lib \
                    -L${libmysqlclient}/lib \
-                   -L${postgresql.lib}/lib \
+                   -L${libpq}/lib \
                    -L${sqlite.out}/lib";
   '';
 
diff --git a/pkgs/development/libraries/gdal/default.nix b/pkgs/development/libraries/gdal/default.nix
index b33641a3442a9..26b40af795774 100644
--- a/pkgs/development/libraries/gdal/default.nix
+++ b/pkgs/development/libraries/gdal/default.nix
@@ -65,7 +65,7 @@
   pcre2,
   pkg-config,
   poppler,
-  postgresql,
+  libpq,
   proj,
   python3,
   qhull,
@@ -163,7 +163,7 @@ stdenv.mkDerivation (finalAttrs: {
         libhwy
       ];
       mysqlDeps = lib.optionals useMysql [ libmysqlclient ];
-      postgresDeps = lib.optionals usePostgres [ postgresql ];
+      postgresDeps = lib.optionals usePostgres [ libpq ];
       popplerDeps = lib.optionals usePoppler [ poppler ];
       arrowDeps = lib.optionals useArrow [ arrow-cpp ];
       hdfDeps = lib.optionals useHDF [
diff --git a/pkgs/development/libraries/kdb/default.nix b/pkgs/development/libraries/kdb/default.nix
index 3364d4926873f..b0ae6dd2af193 100644
--- a/pkgs/development/libraries/kdb/default.nix
+++ b/pkgs/development/libraries/kdb/default.nix
@@ -7,7 +7,7 @@
 , kcoreaddons
 , python3
 , sqlite
-, postgresql
+, libpq
 , libmysqlclient
 , qttools
 }:
@@ -36,7 +36,7 @@ mkDerivation rec {
 
   nativeBuildInputs = [ extra-cmake-modules qttools ];
 
-  buildInputs = [ kcoreaddons python3 sqlite postgresql libmysqlclient ];
+  buildInputs = [ kcoreaddons python3 sqlite libpq libmysqlclient ];
 
   propagatedBuildInputs = [ qtbase ];
 
diff --git a/pkgs/development/libraries/libgda/6.x.nix b/pkgs/development/libraries/libgda/6.x.nix
index 72f4741fd2e76..4d73abd71fa9a 100644
--- a/pkgs/development/libraries/libgda/6.x.nix
+++ b/pkgs/development/libraries/libgda/6.x.nix
@@ -23,11 +23,11 @@
 , mysqlSupport ? false
 , libmysqlclient ? null
 , postgresSupport ? false
-, postgresql ? null
+, libpq ? null
 }:
 
 assert mysqlSupport -> libmysqlclient != null;
-assert postgresSupport -> postgresql != null;
+assert postgresSupport -> libpq != null;
 
 stdenv.mkDerivation rec {
   pname = "libgda";
@@ -82,7 +82,7 @@ stdenv.mkDerivation rec {
   ] ++ lib.optionals mysqlSupport [
     libmysqlclient
   ] ++ lib.optionals postgresSupport [
-    postgresql
+    libpq
   ];
 
   env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.cc.isClang "-Wno-error=incompatible-function-pointer-types";
diff --git a/pkgs/development/libraries/libgda/default.nix b/pkgs/development/libraries/libgda/default.nix
index b6a64c4b7a3fb..62be58b4ee44c 100644
--- a/pkgs/development/libraries/libgda/default.nix
+++ b/pkgs/development/libraries/libgda/default.nix
@@ -19,7 +19,7 @@
 , mysqlSupport ? false
 , libmysqlclient
 , postgresSupport ? false
-, postgresql
+, libpq
 }:
 
 stdenv.mkDerivation rec {
@@ -63,7 +63,7 @@ stdenv.mkDerivation rec {
   ] ++ lib.optionals mysqlSupport [
     libmysqlclient
   ] ++ lib.optionals postgresSupport [
-    postgresql
+    libpq
   ];
 
   propagatedBuildInputs = [
diff --git a/pkgs/development/libraries/librdf/redland.nix b/pkgs/development/libraries/librdf/redland.nix
index 252a5037f77cf..bae6705c63b0a 100644
--- a/pkgs/development/libraries/librdf/redland.nix
+++ b/pkgs/development/libraries/librdf/redland.nix
@@ -1,7 +1,7 @@
 { lib, stdenv, fetchurl, pkg-config, openssl, libxslt, perl
 , curl, pcre, libxml2, librdf_rasqal, gmp
 , libmysqlclient, withMysql ? false
-, postgresql, withPostgresql ? false
+, libpq, withPostgresql ? false
 , sqlite, withSqlite ? true
 , db, withBdb ? false
 }:
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
   buildInputs = [ openssl libxslt curl pcre libxml2 gmp ]
     ++ lib.optional withMysql libmysqlclient
     ++ lib.optional withSqlite sqlite
-    ++ lib.optional withPostgresql postgresql
+    ++ lib.optional withPostgresql libpq
     ++ lib.optional withBdb db;
 
   propagatedBuildInputs = [ librdf_rasqal ];
diff --git a/pkgs/development/libraries/mapnik/default.nix b/pkgs/development/libraries/mapnik/default.nix
index dba5f247bc859..bb69f0a4ac3d4 100644
--- a/pkgs/development/libraries/mapnik/default.nix
+++ b/pkgs/development/libraries/mapnik/default.nix
@@ -22,7 +22,7 @@
   sqlite,
   zlib,
   catch2,
-  postgresql,
+  libpq,
   protozero,
   sparsehash,
 }:
@@ -86,7 +86,7 @@ stdenv.mkDerivation rec {
     sqlite
     zlib
     (libxml2.override { enableHttp = true; })
-    postgresql
+    libpq
     protozero
     sparsehash
   ];
diff --git a/pkgs/development/libraries/qt-5/modules/qtbase.nix b/pkgs/development/libraries/qt-5/modules/qtbase.nix
index 1ca065e94ce93..b489c95b422e6 100644
--- a/pkgs/development/libraries/qt-5/modules/qtbase.nix
+++ b/pkgs/development/libraries/qt-5/modules/qtbase.nix
@@ -12,7 +12,7 @@
 , xcbutilimage, xcbutilkeysyms, xcbutilrenderutil, xcbutilwm , zlib, at-spi2-core
 
   # optional dependencies
-, cups ? null, postgresql ? null
+, cups ? null, libpq ? null
 , withGtk3 ? false, dconf, gtk3
 , withQttranslation ? true, qttranslations ? null
 , withLibinput ? false, libinput
@@ -87,7 +87,7 @@ stdenv.mkDerivation (finalAttrs: ({
     ++ lib.optional developerBuild gdb
     ++ lib.optional (cups != null) cups
     ++ lib.optional (mysqlSupport) libmysqlclient
-    ++ lib.optional (postgresql != null) postgresql;
+    ++ lib.optional (libpq != null) libpq;
 
   nativeBuildInputs = [ bison flex gperf lndir perl pkg-config which ]
     ++ lib.optionals (mysqlSupport) [ libmysqlclient ]
@@ -254,7 +254,7 @@ stdenv.mkDerivation (finalAttrs: ({
   # PostgreSQL autodetection fails sporadically because Qt omits the "-lpq" flag
   # if dependency paths contain the string "pq", which can occur in the hash.
   # To prevent these failures, we need to override PostgreSQL detection.
-  PSQL_LIBS = lib.optionalString (postgresql != null) "-L${postgresql.lib}/lib -lpq";
+  PSQL_LIBS = lib.optionalString (libpq != null) "-L${libpq}/lib -lpq";
 
   } // lib.optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform) {
   configurePlatforms = [ ];
@@ -325,7 +325,7 @@ stdenv.mkDerivation (finalAttrs: ({
     "-I" "${openssl.dev}/include"
     "-system-sqlite"
     ''-${if mysqlSupport then "plugin" else "no"}-sql-mysql''
-    ''-${if postgresql != null then "plugin" else "no"}-sql-psql''
+    ''-${if libpq != null then "plugin" else "no"}-sql-psql''
     "-system-libpng"
 
     "-make libs"
diff --git a/pkgs/development/libraries/qt-6/modules/qtbase.nix b/pkgs/development/libraries/qt-6/modules/qtbase.nix
index b57099ba0c00f..5f32e725b21ef 100644
--- a/pkgs/development/libraries/qt-6/modules/qtbase.nix
+++ b/pkgs/development/libraries/qt-6/modules/qtbase.nix
@@ -73,7 +73,7 @@
   # optional dependencies
 , cups
 , libmysqlclient
-, postgresql
+, libpq
 , withGtk3 ? false
 , dconf
 , gtk3
@@ -168,7 +168,7 @@ stdenv.mkDerivation rec {
   ++ lib.optional withGtk3 gtk3
   ++ lib.optional withLibinput libinput
   ++ lib.optional (libmysqlclient != null && !stdenv.hostPlatform.isMinGW) libmysqlclient
-  ++ lib.optional (postgresql != null && lib.meta.availableOn stdenv.hostPlatform postgresql) postgresql;
+  ++ lib.optional (libpq != null && lib.meta.availableOn stdenv.hostPlatform libpq) libpq;
 
   nativeBuildInputs = [ bison flex gperf lndir perl pkg-config which cmake xmlstarlet ninja ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ moveBuildTree ];
diff --git a/pkgs/development/libraries/soci/default.nix b/pkgs/development/libraries/soci/default.nix
index 3ba387436e764..8577458f66b2d 100644
--- a/pkgs/development/libraries/soci/default.nix
+++ b/pkgs/development/libraries/soci/default.nix
@@ -2,7 +2,7 @@
 , fetchFromGitHub
 , fetchpatch
 , sqlite
-, postgresql
+, libpq
 , boost
 , darwin
 , lib, stdenv
@@ -35,7 +35,7 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = [ cmake ];
   buildInputs = [
     sqlite
-    postgresql
+    libpq
     boost
   ] ++ lib.optionals stdenv.hostPlatform.isDarwin [
     Kerberos
diff --git a/pkgs/development/libraries/wt/default.nix b/pkgs/development/libraries/wt/default.nix
index a58a8ca6b3ecb..827d47253afe2 100644
--- a/pkgs/development/libraries/wt/default.nix
+++ b/pkgs/development/libraries/wt/default.nix
@@ -1,5 +1,5 @@
 { lib, stdenv, fetchFromGitHub, cmake, boost, pkg-config, doxygen, qtbase, libharu
-, pango, fcgi, firebird, libmysqlclient, postgresql, graphicsmagick, glew, openssl
+, pango, fcgi, firebird, libmysqlclient, libpq, graphicsmagick, glew, openssl
 , pcre, harfbuzz, icu
 }:
 
@@ -20,7 +20,7 @@ let
       nativeBuildInputs = [ cmake pkg-config ];
       buildInputs = [
         boost doxygen qtbase libharu
-        pango fcgi firebird libmysqlclient postgresql graphicsmagick glew
+        pango fcgi firebird libmysqlclient libpq graphicsmagick glew
         openssl pcre harfbuzz icu
       ];
 
diff --git a/pkgs/development/lua-modules/overrides.nix b/pkgs/development/lua-modules/overrides.nix
index 62d1cf91c85cd..9923a7aee1bc0 100644
--- a/pkgs/development/lua-modules/overrides.nix
+++ b/pkgs/development/lua-modules/overrides.nix
@@ -44,7 +44,7 @@
 , openssl
 , pcre
 , pkg-config
-, postgresql
+, libpq
 , readline
 , rustPlatform
 , sol2
@@ -361,7 +361,7 @@ in
 
   luadbi-postgresql = prev.luadbi-postgresql.overrideAttrs (oa: {
     buildInputs = oa.buildInputs ++ [
-      (lib.getDev postgresql)
+      (lib.getDev libpq)
     ];
   });
 
diff --git a/pkgs/development/ocaml-modules/postgresql/default.nix b/pkgs/development/ocaml-modules/postgresql/default.nix
index 25dddaed08c9e..ab78b3f524a62 100644
--- a/pkgs/development/ocaml-modules/postgresql/default.nix
+++ b/pkgs/development/ocaml-modules/postgresql/default.nix
@@ -1,4 +1,4 @@
-{ lib, fetchFromGitHub, buildDunePackage, dune-configurator, postgresql }:
+{ lib, fetchFromGitHub, buildDunePackage, dune-configurator, libpq }:
 
 buildDunePackage rec {
   pname = "postgresql";
@@ -15,9 +15,8 @@ buildDunePackage rec {
     sha256 = "1i4pnh2v00i0s7s9pcwz1x6s4xcd77d08gjjkvy0fmda6mqq6ghn";
   };
 
-  nativeBuildInputs = [ postgresql ];
   buildInputs = [ dune-configurator ];
-  propagatedBuildInputs = [ postgresql ];
+  propagatedBuildInputs = [ libpq ];
 
   meta = {
     description = "Bindings to the PostgreSQL library";
diff --git a/pkgs/development/octave-modules/database/default.nix b/pkgs/development/octave-modules/database/default.nix
index 811278af5027e..c757500899fa8 100644
--- a/pkgs/development/octave-modules/database/default.nix
+++ b/pkgs/development/octave-modules/database/default.nix
@@ -2,7 +2,7 @@
 , lib
 , fetchurl
 , struct
-, postgresql
+, libpq
 }:
 
 buildOctavePackage rec {
@@ -19,7 +19,7 @@ buildOctavePackage rec {
   ];
 
   propagatedBuildInputs = [
-    postgresql
+    libpq
   ];
 
   requiredOctavePackages = [
diff --git a/pkgs/development/python-modules/pgsanity/default.nix b/pkgs/development/python-modules/pgsanity/default.nix
index df957c92c6868..19c22876b93d2 100644
--- a/pkgs/development/python-modules/pgsanity/default.nix
+++ b/pkgs/development/python-modules/pgsanity/default.nix
@@ -2,7 +2,7 @@
   lib,
   fetchPypi,
   buildPythonPackage,
-  postgresql,
+  libpq,
   unittestCheckHook,
 }:
 
@@ -18,12 +18,11 @@ buildPythonPackage rec {
 
   nativeCheckInputs = [
     unittestCheckHook
-    postgresql
   ];
 
   unittestFlagsArray = [ "test" ];
 
-  propagatedBuildInputs = [ postgresql ];
+  propagatedBuildInputs = [ libpq ];
 
   meta = with lib; {
     homepage = "https://github.com/markdrago/pgsanity";
diff --git a/pkgs/development/python-modules/psycopg/default.nix b/pkgs/development/python-modules/psycopg/default.nix
index 33cc96524f567..22679d49e58f5 100644
--- a/pkgs/development/python-modules/psycopg/default.nix
+++ b/pkgs/development/python-modules/psycopg/default.nix
@@ -8,7 +8,7 @@
   substituteAll,
 
   # build
-  postgresql,
+  libpq,
   setuptools,
 
   # propagates
@@ -29,6 +29,7 @@
   pproxy,
   pytest-randomly,
   pytestCheckHook,
+  postgresql,
   postgresqlTestHook,
 }:
 
@@ -46,7 +47,7 @@ let
   patches = [
     (substituteAll {
       src = ./ctypes.patch;
-      libpq = "${postgresql.lib}/lib/libpq${stdenv.hostPlatform.extensions.sharedLibrary}";
+      libpq = "${libpq}/lib/libpq${stdenv.hostPlatform.extensions.sharedLibrary}";
       libc = "${stdenv.cc.libc}/lib/libc.so.6";
     })
   ];
@@ -74,13 +75,13 @@ let
     nativeBuildInputs = [
       cython
       # needed to find pg_config with strictDeps
-      postgresql
+      libpq
       setuptools
       tomli
     ];
 
     buildInputs = [
-      postgresql
+      libpq
     ];
 
     # tested in psycopg
diff --git a/pkgs/development/ruby-modules/gem-config/default.nix b/pkgs/development/ruby-modules/gem-config/default.nix
index 0c34601fd7259..40cc9efd8ba07 100644
--- a/pkgs/development/ruby-modules/gem-config/default.nix
+++ b/pkgs/development/ruby-modules/gem-config/default.nix
@@ -18,7 +18,7 @@
 # (to make gems behave if necessary).
 
 { lib, fetchurl, fetchpatch2, writeScript, ruby, libkrb5, libxml2, libxslt, python2, stdenv, which
-, libiconv, postgresql, nodejs, clang, sqlite, zlib, imagemagick, lasem
+, libiconv, libpq, nodejs, clang, sqlite, zlib, imagemagick, lasem
 , pkg-config , ncurses, xapian, gpgme, util-linux, tzdata, icu, libffi
 , cmake, libssh2, openssl, openssl_1_1, libmysqlclient, git, perl, pcre2, gecode_3, curl
 , libsodium, snappy, libossp_uuid, lxc, libpcap, xorg, gtk3, lerc, buildRubyGem
@@ -654,7 +654,7 @@ in
     # an unnecessary reference to the entire postgresql package.
     buildFlags = [ "--with-pg-config=ignore" ];
     nativeBuildInputs = [ pkg-config ];
-    buildInputs = [ postgresql ];
+    buildInputs = [ libpq ];
   };
 
   psych = attrs: {
@@ -807,7 +807,7 @@ in
   };
 
   sequel_pg = attrs: {
-    buildInputs = [ postgresql ];
+    buildInputs = [ libpq ];
   };
 
   snappy = attrs: {
diff --git a/pkgs/games/ldmud/default.nix b/pkgs/games/ldmud/default.nix
index de92518781fe1..8287b3274c7d9 100644
--- a/pkgs/games/ldmud/default.nix
+++ b/pkgs/games/ldmud/default.nix
@@ -16,7 +16,7 @@
 , mysqlSupport ? false
 , libmysqlclient
 , postgresSupport ? false
-, postgresql
+, libpq
 , sqliteSupport ? false
 , sqlite
 , tlsSupport ? false
@@ -43,7 +43,7 @@ stdenv.mkDerivation rec {
     [ autoreconfHook pkg-config bison ];
   buildInputs = [ libgcrypt libxcrypt pcre json_c libxml2 ]
     ++ lib.optional mccpSupport zlib ++ lib.optional mysqlSupport libmysqlclient
-    ++ lib.optional postgresSupport postgresql
+    ++ lib.optional postgresSupport libpq
     ++ lib.optional sqliteSupport sqlite ++ lib.optional tlsSupport openssl
     ++ lib.optional pythonSupport python310
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ];
diff --git a/pkgs/games/minetest/default.nix b/pkgs/games/minetest/default.nix
index de566217b1baa..e93eb6cd4f89d 100644
--- a/pkgs/games/minetest/default.nix
+++ b/pkgs/games/minetest/default.nix
@@ -24,7 +24,7 @@
 , gmp
 , libspatialindex
 , leveldb
-, postgresql
+, libpq
 , hiredis
 , libiconv
 , ninja
@@ -113,7 +113,7 @@ stdenv.mkDerivation (finalAttrs: {
     xorg.libXi
   ] ++ lib.optionals buildServer [
     leveldb
-    postgresql
+    libpq
     hiredis
     prometheus-cpp
   ];
diff --git a/pkgs/servers/http/openresty/default.nix b/pkgs/servers/http/openresty/default.nix
index 65191c6d713aa..a7e2f939c709b 100644
--- a/pkgs/servers/http/openresty/default.nix
+++ b/pkgs/servers/http/openresty/default.nix
@@ -3,7 +3,7 @@
 , lib
 , fetchurl
 , perl
-, postgresql
+, libpq
 , nixosTests
 , ...
 }@args:
@@ -31,7 +31,7 @@ callPackage ../nginx/generic.nix args rec {
 
   nativeBuildInputs = [ perl ];
 
-  buildInputs = [ postgresql ];
+  buildInputs = [ libpq ];
 
   postPatch = ''
     patchShebangs configure bundle/
diff --git a/pkgs/servers/mail/dovecot/default.nix b/pkgs/servers/mail/dovecot/default.nix
index 7a2c42fad1f9f..e83917efbcaa4 100644
--- a/pkgs/servers/mail/dovecot/default.nix
+++ b/pkgs/servers/mail/dovecot/default.nix
@@ -5,7 +5,7 @@
 , fetchpatch
 # Auth modules
 , withMySQL ? false, libmysqlclient
-, withPgSQL ? false, postgresql
+, withPgSQL ? false, libpq
 , withSQLite ? true, sqlite
 , withLua ? false, lua5_3
 }:
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
     [ openssl bzip2 zlib lz4 clucene_core_2 icu openldap libsodium libstemmer cyrus_sasl.dev ]
     ++ lib.optionals (stdenv.hostPlatform.isLinux) [ systemd pam libcap inotify-tools ]
     ++ lib.optional withMySQL libmysqlclient
-    ++ lib.optional withPgSQL postgresql
+    ++ lib.optional withPgSQL libpq
     ++ lib.optional withSQLite sqlite
     ++ lib.optional withLua lua5_3;
 
diff --git a/pkgs/servers/mail/opensmtpd/extras.nix b/pkgs/servers/mail/opensmtpd/extras.nix
index f179bf96368ff..82c86edff321a 100644
--- a/pkgs/servers/mail/opensmtpd/extras.nix
+++ b/pkgs/servers/mail/opensmtpd/extras.nix
@@ -1,5 +1,5 @@
 { lib, stdenv, fetchurl, openssl, libevent, libasr, ncurses,
-  pkg-config, lua5, perl, libmysqlclient, postgresql, sqlite, hiredis,
+  pkg-config, lua5, perl, libmysqlclient, libpq, sqlite, hiredis,
   enableLua ? true,
   enablePerl ? true,
   enableMysql ? true,
@@ -19,7 +19,7 @@ stdenv.mkDerivation rec {
 
   nativeBuildInputs = [ pkg-config ];
   buildInputs = [ openssl libevent
-    libasr lua5 perl libmysqlclient postgresql sqlite hiredis ];
+    libasr lua5 perl libmysqlclient libpq sqlite hiredis ];
 
   configureFlags = [
     "--sysconfdir=/etc"
diff --git a/pkgs/servers/mail/postfix/default.nix b/pkgs/servers/mail/postfix/default.nix
index 4bd224039b66a..f29300938b7a1 100644
--- a/pkgs/servers/mail/postfix/default.nix
+++ b/pkgs/servers/mail/postfix/default.nix
@@ -3,7 +3,7 @@
 , fetchpatch
 , buildPackages, nixosTests
 , withLDAP ? true, openldap
-, withPgSQL ? false, postgresql
+, withPgSQL ? false, libpq
 , withMySQL ? false, libmysqlclient
 , withSQLite ? false, sqlite
 }:
@@ -34,7 +34,7 @@ in stdenv.mkDerivation rec {
 
   nativeBuildInputs = [ makeWrapper m4 ];
   buildInputs = [ db openssl cyrus_sasl icu libnsl pcre2 ]
-    ++ lib.optional withPgSQL postgresql
+    ++ lib.optional withPgSQL libpq
     ++ lib.optional withMySQL libmysqlclient
     ++ lib.optional withSQLite sqlite
     ++ lib.optional withLDAP openldap;
diff --git a/pkgs/servers/monitoring/icinga2/default.nix b/pkgs/servers/monitoring/icinga2/default.nix
index 1b833e9ccbafe..60de55a657b62 100644
--- a/pkgs/servers/monitoring/icinga2/default.nix
+++ b/pkgs/servers/monitoring/icinga2/default.nix
@@ -1,5 +1,5 @@
 { stdenv, runCommand, lib, fetchFromGitHub, cmake, flex, bison, systemd
-, boost, openssl, patchelf, mariadb-connector-c, postgresql, zlib, tzdata
+, boost, openssl, patchelf, mariadb-connector-c, libpq, zlib, tzdata
 # Databases
 , withMysql ? true, withPostgresql ? false
 # Features
@@ -55,7 +55,7 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "doc" ];
 
   buildInputs = [ boost openssl systemd ]
-    ++ lib.optional withPostgresql postgresql;
+    ++ lib.optional withPostgresql libpq;
 
   nativeBuildInputs = [ cmake flex bison patchelf ];
 
diff --git a/pkgs/servers/monitoring/zabbix/proxy.nix b/pkgs/servers/monitoring/zabbix/proxy.nix
index c0ac7d46628b2..c36ea2d16b5c8 100644
--- a/pkgs/servers/monitoring/zabbix/proxy.nix
+++ b/pkgs/servers/monitoring/zabbix/proxy.nix
@@ -19,7 +19,7 @@
   mysqlSupport ? false,
   libmysqlclient,
   postgresqlSupport ? false,
-  postgresql,
+  libpq,
 }:
 
 # ensure exactly one database type is selected
@@ -55,7 +55,7 @@ import ./versions.nix (
       ++ optional sqliteSupport sqlite
       ++ optional sshSupport libssh2
       ++ optional mysqlSupport libmysqlclient
-      ++ optional postgresqlSupport postgresql;
+      ++ optional postgresqlSupport libpq;
 
     configureFlags =
       [
diff --git a/pkgs/servers/monitoring/zabbix/server.nix b/pkgs/servers/monitoring/zabbix/server.nix
index 9234354d4366d..d3edb9d1b733f 100644
--- a/pkgs/servers/monitoring/zabbix/server.nix
+++ b/pkgs/servers/monitoring/zabbix/server.nix
@@ -24,7 +24,7 @@
   mysqlSupport ? false,
   libmysqlclient,
   postgresqlSupport ? false,
-  postgresql,
+  libpq,
   ipmiSupport ? false,
   openipmi,
 }:
@@ -67,7 +67,7 @@ import ./versions.nix (
       ++ optional snmpSupport net-snmp
       ++ optional sshSupport libssh2
       ++ optional mysqlSupport libmysqlclient
-      ++ optional postgresqlSupport postgresql
+      ++ optional postgresqlSupport libpq
       ++ optional ipmiSupport openipmi;
 
     configureFlags =
diff --git a/pkgs/servers/mx-puppet-discord/default.nix b/pkgs/servers/mx-puppet-discord/default.nix
index 1d86bc472a368..6e3cedff3f219 100644
--- a/pkgs/servers/mx-puppet-discord/default.nix
+++ b/pkgs/servers/mx-puppet-discord/default.nix
@@ -1,5 +1,5 @@
 { stdenv, fetchFromGitLab, pkgs, lib, node-pre-gyp, nodejs_18, pkg-config
-, libjpeg, pixman, cairo, pango, which, postgresql }:
+, libjpeg, pixman, cairo, pango, which, libpq }:
 
 let
   nodejs = nodejs_18;
@@ -23,7 +23,7 @@ in myNodePackages.package.override {
   inherit version src;
 
   nativeBuildInputs = [ node-pre-gyp nodejs.pkgs.node-gyp-build pkg-config which ];
-  buildInputs = [ libjpeg pixman cairo pango postgresql ];
+  buildInputs = [ libjpeg pixman cairo pango libpq ];
 
   postRebuild = ''
     # Build typescript stuff
diff --git a/pkgs/servers/nominatim/default.nix b/pkgs/servers/nominatim/default.nix
index 529849625d93b..aaed99bda470f 100644
--- a/pkgs/servers/nominatim/default.nix
+++ b/pkgs/servers/nominatim/default.nix
@@ -1,7 +1,7 @@
 { stdenv, lib, fetchFromGitHub, fetchurl
 , clang-tools, cmake, bzip2, zlib, expat, boost, git, pandoc, nlohmann_json
 # Nominatim needs to be built with the same postgres version it will target
-, postgresql
+, libpq
 , python3, php, lua
 }:
 
@@ -54,7 +54,7 @@ stdenv.mkDerivation rec {
     # python3Packages.pylint  # We don't want to run pylint because the package could break on pylint bumps which is really annoying.
     # python3Packages.pytest  # disabled since I can't get it to run tests anyway
     # python3Packages.behave  # disabled since I can't get it to run tests anyway
-    postgresql
+    libpq
   ];
 
   postPatch = ''
diff --git a/pkgs/servers/sip/freeswitch/modules.nix b/pkgs/servers/sip/freeswitch/modules.nix
index 12d71e6d749cb..84ed7f1d25180 100644
--- a/pkgs/servers/sip/freeswitch/modules.nix
+++ b/pkgs/servers/sip/freeswitch/modules.nix
@@ -8,7 +8,7 @@
 , curl
 , ffmpeg
 , libmysqlclient
-, postgresql
+, libpq
 , spandsp3
 , sofia_sip
 , libks
@@ -114,7 +114,7 @@ in
 
   databases = {
     mariadb = mk "databases/mod_mariadb" [ libmysqlclient ];
-    pgsql = mk "databases/mod_pgsql" [ postgresql ];
+    pgsql = mk "databases/mod_pgsql" [ libpq ];
   };
 
   dialplans = {
diff --git a/pkgs/servers/sql/postgresql/default.nix b/pkgs/servers/sql/postgresql/default.nix
index 56c0158eca6d1..5d231bf7cd032 100644
--- a/pkgs/servers/sql/postgresql/default.nix
+++ b/pkgs/servers/sql/postgresql/default.nix
@@ -26,6 +26,8 @@ let
       })
     ) versions;
 
+  libpq = self.callPackage ./libpq.nix {};
+
 in
 # variations without and with JIT
-(mkAttributes false) // (mkAttributes true)
+(mkAttributes false) // (mkAttributes true) // { inherit libpq; }
diff --git a/pkgs/servers/sql/postgresql/libpq.nix b/pkgs/servers/sql/postgresql/libpq.nix
new file mode 100644
index 0000000000000..91e9de0f4b91a
--- /dev/null
+++ b/pkgs/servers/sql/postgresql/libpq.nix
@@ -0,0 +1,129 @@
+{
+  # utils
+  stdenv,
+  fetchurl,
+  lib,
+
+  # runtime dependencies
+  openssl,
+  tzdata,
+  zlib,
+
+  # build dependencies
+  bison,
+  flex,
+  perl,
+  pkg-config,
+
+  # passthru / meta
+  postgresql,
+
+  # GSSAPI
+  gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic,
+  libkrb5,
+
+  # NLS
+  nlsSupport ? false,
+  gettext,
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+  pname = "libpq";
+  version = "17.2";
+
+  src = fetchurl {
+    url = "mirror://postgresql/source/v${finalAttrs.version}/postgresql-${finalAttrs.version}.tar.bz2";
+    hash = "sha256-gu8nwK83UWldf2Ti2WNYMAX7tqDD32PQ5LQiEdcCEWQ=";
+  };
+
+  __structuredAttrs = true;
+
+  hardeningEnable = lib.optionals (!stdenv.cc.isClang) [ "pie" ];
+
+  outputs = [
+    "out"
+    "dev"
+  ];
+  outputChecks.out = {
+    disallowedReferences = [ "dev" ];
+    disallowedRequisites = [
+      stdenv.cc
+    ] ++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs));
+  };
+
+  buildInputs = [
+    zlib
+    openssl
+  ] ++ lib.optionals gssSupport [ libkrb5 ] ++ lib.optionals nlsSupport [ gettext ];
+
+  nativeBuildInputs = [
+    bison
+    flex
+    perl
+    pkg-config
+  ];
+
+  # causes random build failures
+  enableParallelBuilding = false;
+
+  separateDebugInfo = true;
+
+  buildFlags = [
+    "submake-libpgport"
+    "submake-libpq"
+  ];
+
+  # libpgcommon.a and libpgport.a contain all paths normally returned by pg_config and are
+  # linked into all shared libraries. However, almost no binaries actually use those paths.
+  # The following flags will remove unused sections from all shared libraries - including
+  # those paths. This avoids a lot of circular dependency problems with different outputs,
+  # and allows splitting them cleanly.
+  env.CFLAGS =
+    "-fdata-sections -ffunction-sections"
+    + (if stdenv.cc.isClang then " -flto" else " -fmerge-constants -Wl,--gc-sections");
+
+  configureFlags = [
+    "--enable-debug"
+    "--sysconfdir=/etc"
+    "--with-openssl"
+    "--with-system-tzdata=${tzdata}/share/zoneinfo"
+    "--without-icu"
+    "--without-perl"
+    "--without-readline"
+  ] ++ lib.optionals gssSupport [ "--with-gssapi" ] ++ lib.optionals nlsSupport [ "--enable-nls" ];
+
+  patches = lib.optionals stdenv.hostPlatform.isLinux [
+    ./patches/socketdir-in-run-13+.patch
+  ];
+
+  installPhase = ''
+    runHook preInstall
+
+    make -C src/bin/pg_config install
+    make -C src/common install
+    make -C src/include install
+    make -C src/interfaces/libpq install
+    make -C src/port install
+
+    moveToOutput bin/pg_config "$dev"
+    moveToOutput "lib/*.a" "$dev"
+
+    rm -rfv $out/share
+    rm -rfv $dev/lib/*_shlib.a
+
+    runHook postInstall
+  '';
+
+  # PostgreSQL always builds both shared and static libs, so we delete those we don't want.
+  postInstall =
+    if stdenv.hostPlatform.isStatic then "rm -rfv $out/lib/*.so*" else "rm -rfv $dev/lib/*.a";
+
+  doCheck = false;
+
+  meta = postgresql.meta // {
+    description = "C application programmer's interface to PostgreSQL";
+    changelog = "https://www.postgresql.org/docs/release/${finalAttrs.version}/";
+    pkgConfigModules = [ "libpq" ];
+    broken = false;
+  };
+})
diff --git a/pkgs/servers/web-apps/lemmy/server.nix b/pkgs/servers/web-apps/lemmy/server.nix
index 8c9407afff444..6f76e3e207153 100644
--- a/pkgs/servers/web-apps/lemmy/server.nix
+++ b/pkgs/servers/web-apps/lemmy/server.nix
@@ -3,7 +3,7 @@
 , rustPlatform
 , fetchFromGitHub
 , openssl
-, postgresql
+, libpq
 , libiconv
 , Security
 , SystemConfiguration
@@ -33,7 +33,7 @@ rustPlatform.buildRustPackage rec {
 
   cargoHash = pinData.serverCargoHash;
 
-  buildInputs = [ postgresql ]
+  buildInputs = [ libpq ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv Security SystemConfiguration ];
 
   # Using OPENSSL_NO_VENDOR is not an option on darwin
diff --git a/pkgs/tools/backup/bacula/default.nix b/pkgs/tools/backup/bacula/default.nix
index 5ab53918d1495..c8d5524abdc0c 100644
--- a/pkgs/tools/backup/bacula/default.nix
+++ b/pkgs/tools/backup/bacula/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, sqlite, postgresql, zlib, acl, ncurses, openssl, readline
+{ lib, stdenv, fetchurl, sqlite, libpq, zlib, acl, ncurses, openssl, readline
 , gettext, CoreFoundation, IOKit, Kerberos
 }:
 
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
       --replace "10.*)" "*)"
   '';
 
-  buildInputs = [ postgresql sqlite zlib ncurses openssl readline ]
+  buildInputs = [ libpq sqlite zlib ncurses openssl readline ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [
       gettext # bacula requires CoreFoundation, but its `configure` script will only link it when it detects libintl.
       CoreFoundation
@@ -29,7 +29,7 @@ stdenv.mkDerivation rec {
 
   configureFlags = [
     "--with-sqlite3=${sqlite.dev}"
-    "--with-postgresql=${lib.getDev postgresql}"
+    "--with-postgresql=${lib.getDev libpq}"
     "--with-logdir=/var/log/bacula"
     "--with-working-dir=/var/lib/bacula"
     "--mandir=\${out}/share/man"
diff --git a/pkgs/tools/package-management/disnix/dysnomia/default.nix b/pkgs/tools/package-management/disnix/dysnomia/default.nix
index 92b880d1428fd..526912128cfbf 100644
--- a/pkgs/tools/package-management/disnix/dysnomia/default.nix
+++ b/pkgs/tools/package-management/disnix/dysnomia/default.nix
@@ -1,7 +1,7 @@
 { lib, stdenv, fetchurl, netcat
 
 # Optional packages
-, systemd ? null, ejabberd ? null, mariadb ? null, postgresql ? null, subversion ? null
+, systemd ? null, ejabberd ? null, mariadb ? null, libpq ? null, subversion ? null
 , mongodb ? null, mongodb-tools ? null, influxdb ? null, supervisor ? null, docker ? null
 , nginx ? null, s6-rc ? null, xinetd ? null
 
@@ -27,7 +27,7 @@
 }:
 
 assert enableMySQLDatabase -> mariadb != null;
-assert enablePostgreSQLDatabase -> postgresql != null;
+assert enablePostgreSQLDatabase -> libpq != null;
 assert enableSubversionRepository -> subversion != null;
 assert enableEjabberdDump -> ejabberd != null;
 assert enableMongoDatabase -> (mongodb != null && mongodb-tools != null);
@@ -51,7 +51,7 @@ stdenv.mkDerivation rec {
      (if enableAxis2WebService then "--with-axis2" else "--without-axis2")
      (if enableEjabberdDump then "--with-ejabberd" else "--without-ejabberd")
      (if enableMySQLDatabase then "--with-mysql" else "--without-mysql")
-     (if enablePostgreSQLDatabase then "--with-postgresql" else "--without-postgresql")
+     (if enablePostgreSQLDatabase then "--with-postgresql" else "--without-libpq")
      (if enableSubversionRepository then "--with-subversion" else "--without-subversion")
      (if enableTomcatWebApplication then "--with-tomcat=${catalinaBaseDir}" else "--without-tomcat")
      (if enableMongoDatabase then "--with-mongodb" else "--without-mongodb")
@@ -69,7 +69,7 @@ stdenv.mkDerivation rec {
     ++ lib.optional stdenv.hostPlatform.isLinux systemd
     ++ lib.optional enableEjabberdDump ejabberd
     ++ lib.optional enableMySQLDatabase mariadb.out
-    ++ lib.optional enablePostgreSQLDatabase postgresql
+    ++ lib.optional enablePostgreSQLDatabase libpq
     ++ lib.optional enableSubversionRepository subversion
     ++ lib.optionals enableMongoDatabase [ mongodb mongodb-tools ]
     ++ lib.optional enableInfluxDatabase influxdb
diff --git a/pkgs/tools/security/vaultwarden/default.nix b/pkgs/tools/security/vaultwarden/default.nix
index e0039281261fd..4c5617687c7c5 100644
--- a/pkgs/tools/security/vaultwarden/default.nix
+++ b/pkgs/tools/security/vaultwarden/default.nix
@@ -1,7 +1,7 @@
 { lib, stdenv, callPackage, rustPlatform, fetchFromGitHub, nixosTests
 , pkg-config, openssl
 , libiconv, Security, CoreServices, SystemConfiguration
-, dbBackend ? "sqlite", libmysqlclient, postgresql }:
+, dbBackend ? "sqlite", libmysqlclient, libpq }:
 
 let
   webvault = callPackage ./webvault.nix {};
@@ -27,7 +27,7 @@ rustPlatform.buildRustPackage rec {
   buildInputs = [ openssl ]
     ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv Security CoreServices SystemConfiguration ]
     ++ lib.optional (dbBackend == "mysql") libmysqlclient
-    ++ lib.optional (dbBackend == "postgresql") postgresql;
+    ++ lib.optional (dbBackend == "postgresql") libpq;
 
   buildFeatures = dbBackend;
 
diff --git a/pkgs/tools/system/collectd/plugins.nix b/pkgs/tools/system/collectd/plugins.nix
index 7202ec795e8b2..b81aa78c5b5d7 100644
--- a/pkgs/tools/system/collectd/plugins.nix
+++ b/pkgs/tools/system/collectd/plugins.nix
@@ -27,7 +27,7 @@
 , openldap
 , openipmi
 , perl
-, postgresql
+, libpq
 , protobufc
 , python3
 , rabbitmq-c
@@ -99,7 +99,7 @@ let
     perl.buildInputs = [ perl ];
     pinba.buildInputs = [ protobufc ];
     ping.buildInputs = [ liboping ];
-    postgresql.buildInputs = [ postgresql ];
+    postgresql.buildInputs = [ libpq ];
     python.buildInputs = [ python3 ];
     redis.buildInputs = [ hiredis ];
     rrdcached.buildInputs = [ rrdtool libxml2 ];
diff --git a/pkgs/tools/system/rsyslog/default.nix b/pkgs/tools/system/rsyslog/default.nix
index 451ced45d728c..2414145aa9afd 100644
--- a/pkgs/tools/system/rsyslog/default.nix
+++ b/pkgs/tools/system/rsyslog/default.nix
@@ -17,7 +17,7 @@
 , withMysql ? true
 , libmysqlclient
 , withPostgres ? true
-, postgresql
+, libpq
 , withDbi ? true
 , libdbi
 , withNetSnmp ? true
@@ -81,7 +81,7 @@ stdenv.mkDerivation rec {
     zlib
   ] ++ lib.optional withKrb5 libkrb5
   ++ lib.optional withJemalloc jemalloc
-  ++ lib.optional withPostgres postgresql
+  ++ lib.optional withPostgres libpq
   ++ lib.optional withDbi libdbi
   ++ lib.optional withNetSnmp net-snmp
   ++ lib.optional withUuid libuuid
diff --git a/pkgs/tools/text/gawk/gawkextlib.nix b/pkgs/tools/text/gawk/gawkextlib.nix
index eaf7799a4860e..6836e1fb9b68d 100644
--- a/pkgs/tools/text/gawk/gawkextlib.nix
+++ b/pkgs/tools/text/gawk/gawkextlib.nix
@@ -1,6 +1,6 @@
 { lib, stdenv, recurseIntoAttrs, fetchgit, pkg-config, autoreconfHook
 , autoconf, automake, libiconv, libtool, texinfo, gettext, gawk, rapidjson, gd
-, libharu, lmdb, gmp, glibcLocales, mpfr, more, postgresql, hiredis
+, libharu, lmdb, gmp, glibcLocales, mpfr, more, libpq, hiredis
 , expat, tre }:
 
 let
@@ -130,7 +130,7 @@ let
     pgsql = buildExtension {
       inherit gawkextlib;
       name = "pgsql";
-      extraBuildInputs = [ postgresql ];
+      extraBuildInputs = [ libpq ];
     };
     redis = buildExtension {
       inherit gawkextlib;
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 8fd968f959b10..44e26765acc4f 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -1044,9 +1044,7 @@ with pkgs;
     systemd = pkgs.systemd;
   };
 
-  nominatim = callPackage ../servers/nominatim {
-    postgresql = postgresql_14;
-  };
+  nominatim = callPackage ../servers/nominatim { };
 
   ocs-url = libsForQt5.callPackage ../tools/misc/ocs-url { };
 
@@ -11973,6 +11971,8 @@ with pkgs;
 
   postgresqlVersions = import ../servers/sql/postgresql pkgs;
   inherit (postgresqlVersions)
+    libpq
+
     postgresql_13
     postgresql_14
     postgresql_15
diff --git a/pkgs/top-level/ocaml-packages.nix b/pkgs/top-level/ocaml-packages.nix
index 6f244496f514f..b6128226b9aa4 100644
--- a/pkgs/top-level/ocaml-packages.nix
+++ b/pkgs/top-level/ocaml-packages.nix
@@ -1514,7 +1514,7 @@ let
     posix-types = callPackage ../development/ocaml-modules/posix/types.nix { };
 
     postgresql = callPackage ../development/ocaml-modules/postgresql {
-      inherit (pkgs) postgresql;
+      inherit (pkgs) libpq;
     };
 
     pp = callPackage ../development/ocaml-modules/pp { };
diff --git a/pkgs/top-level/php-packages.nix b/pkgs/top-level/php-packages.nix
index dd925256e3ab7..813667d2f827e 100644
--- a/pkgs/top-level/php-packages.nix
+++ b/pkgs/top-level/php-packages.nix
@@ -32,7 +32,7 @@
 , overrideSDK
 , pam
 , pcre2
-, postgresql
+, libpq
 , bison
 , re2c
 , readline
@@ -553,7 +553,8 @@ in {
         {
           name = "pdo_pgsql";
           internalDeps = [ php.extensions.pdo ];
-          configureFlags = [ "--with-pdo-pgsql=${lib.getDev postgresql}" ];
+          buildInputs = [ libpq ];
+          configureFlags = [ "--with-pdo-pgsql=${lib.getDev libpq}" ];
           doCheck = false;
         }
         {
@@ -565,8 +566,8 @@ in {
         }
         {
           name = "pgsql";
-          buildInputs = [ pcre2 ];
-          configureFlags = [ "--with-pgsql=${lib.getDev postgresql}" ];
+          buildInputs = [ libpq pcre2 ];
+          configureFlags = [ "--with-pgsql=${lib.getDev libpq}" ];
           doCheck = false;
         }
         { name = "posix"; doCheck = false; }