0

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.

Lolly
  • 34,250
  • 42
  • 115
  • 150

2 Answers2

1

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.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
0

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
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • But how do I know the path to the installation directory ? Since it will installed in client's system, it can in anywhere. – Lolly Oct 15 '12 at 12:29
  • You ask client to configure `POSTGRES_HOME` in your Environment variables the same way `JAVA_HOME` is configured by specifying path to installation directory. And you use same variable to find path. If path not found then search on expected location. – Amit Deshpande Oct 15 '12 at 12:32