Matthias Loeks
Matthias Loeks

Reputation: 305

How to find the PostgreSQL installation folder via Java without user interaction?

In my Java application I want to implement the option to dump/restore a PostgreSQL database. Some google research showed me that calling pg_dump/pg_restore via Java's ProcessBuilder is probably the best way to achieve this.

Now I'd like to determine the system's directory of pg_dump.exe/pg_restore.exe without asking the user to specify it manually. How can I find the PostgreSQL installation path through Java?

Thanks in advance and all the best,

Matthias

Upvotes: 2

Views: 1753

Answers (3)

Peter Eisentraut
Peter Eisentraut

Reputation: 36729

You can't do that portably. I suggest that you just call the programs without path by default and rely on the user having set an appropriate path, and have a way for the user to configure the paths explicitly. You can also look in some likely directories for a default configuration. But it will be quite fragile in general, especially if you want to cover all of Windows, Linux, and Mac, say.

Upvotes: 1

carlspring
carlspring

Reputation: 32617

A good idea would be to simply define an environment variable called PG_HOME and use System.getEnv("PG_HOME"); to retrieve that.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340753

  1. Look inside the default directories first (probably somewhere C:\Program Files (x86)\PostgreSQL\...). Use different combinations to be as tolerable as possible.

  2. If it fails, just search the file system. Nice example here.

  3. Also see How to do a backup from a Postgresql-DB via JDBC?

Upvotes: 0

Related Questions