Lolly
Lolly

Reputation: 36422

Using Java to find Postgresql installation location

I want to find out the installation location of Postgresql and need to make some changes in postgresql.conf and pg_hba.conf files and restart Postgresql.

Is there any way to identify the installation directory which can be applied to Windows/Unix/Linux ? Any help or suggestion will be really appreciated.

Upvotes: 0

Views: 786

Answers (2)

Craig Ringer
Craig Ringer

Reputation: 324501

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.

Upvotes: 1

Amit Deshpande
Amit Deshpande

Reputation: 19185

You can define custom environment variable like POSTGRES_HOME and use it similar to JAVA_HOME

 POSTGRES_HOME  = /usr/local/pgsql
 pg_hba.conf path =  POSTGRES_HOME/data/pg_hba.conf

Upvotes: 0

Related Questions