Ask PostgreSQL:
SHOW config_file;
SHOW hba_file;
or:
SELECT current_setting('config_file');
SELECT current_setting('hba_file');
This, of course, requires PostgreSQL to be running, listening on TCP/IP, and on the default port. It also requires you to be able to authenticate. You would need to prompt the user for the port (default to 5432) and credentials.
There is no generic way to determine the PostgreSQL data directory location without asking a running PostgreSQL instance, because:
- There might be more than one running PostgreSQL instance
- PostgreSQL on Mac OS X might've been installed via Homebrew (Mac OS X), Postgres.app (Mac OS X), system package management (Linux), ports (BSD), the EnterpriseDB installer, various 3rd party rollup distributions, the .zip binaries, or from source.
- PostgreSQL might be started and run from launchd (Mac OS X), as a Windows service (Windows), from systemd/upstart/init (Linux/BSD), manually by the user with
pg_ctl
, etc. A data directory can be specified on the startup command line.
So the whole idea that you can figure out "the" location of the PostgreSQL datadir is bogus.
If you're writing software that relies on PostgreSQL, consider bundling your own copy with the .zip binaries, starting it with pg_ctl
and running it on a non-default port so it doesn't interfere with any PostgreSQL install the user may've put on their system themselves.