This will detect PGDATA for the postgresql systemd unit.
(set -euo pipefail ; eval "$(systemctl show -p Environment postgresql | grep -oP '(?<=^Environment=).*' || echo false)" ; echo $PGDATA)
Inspired by how postgresql-setup script detects the correct location.