First thing you could try (after normal removal fails) is to force removal with dpkg
:
dpkg --remove --force-remove-reinstreq postgresql-14
If that also fails you have to dig deeper. According to the error messages the problem is with the maintainer scripts of the package. These (among other things) are unpacked to /var/lib/dpkg/info
when a package is installed and get invoked from that directory.
If you want to debug the issue, check the content of the respective maintainer script (pre-removal script for your PostgreSQL package for instance would be postgresql-14.prerm
) and follow the trail from there, i.e. check what commands the script invokes and/or what other files it sources to pinpoint what exactly is failing¹.
If you just want the package removed, simply replace the code after the shebang with an exit 0
#!/bin/sh
exit 0
and then re-run package removal
apt-get remove --auto-remove --purge postgresql-14
¹ TWIMC: In our case the root cause turned out to be an artifact from replacing Systemd with a different init (s6). PostgreSQL maintainer scripts apparently distinguish between Systemd and other inits by the presence of a directory /run/systemd/system
, which had not been removed after replacing the init on the system in question. After manually removing that directory the package could be (un)installed normally.