4

I am using the below Java statement to start the PostgreSQL server from Java code. I am using Runtime.getRuntime().exec() method to achieve this.

 final Process startServer = Runtime.getRuntime().exec("PostgreQL -D data/dir/ start");

Here when the server is SSL enabled and if the key is password protected, in command line it prompts for password (see below lines). In this case how can I pass the password.

Is server running?
starting server anyway
waiting for server to start......Enter PEM pass phrase:....

Do we have any option to pass the password as a parameter, when prompted? or in PostgreSQL while starting do we have any provision to pass the keypassword (like PostgreSQL -d data/dir/ -keyPass password start)?

halfer
  • 19,824
  • 17
  • 99
  • 186
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • http://stackoverflow.com/questions/4112470/java-how-to-both-read-and-write-to-from-process-thru-pipe-stdin-stdout – Jayan Feb 01 '13 at 04:54

3 Answers3

4

You can open the OutputStream of the process and pass the passphrase which will be read by the server as if you typed it is coming from STDIN.

final Process startServer = Runtime.getRuntime().exec("PostgreQL -D data/dir/ start");
PrintStream ps = new PrintStream(startServer.getOutputStream());
ps.println(passPhrase);
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Vikdor: Thanks for your time. So ps.println(passPhrase) will pass the password to command line is it ? – Lolly Feb 01 '13 at 05:00
  • One more question, can this parameter (ps.println(passPhrase)) can be passed at beginning itself or should be passed only when it is prompted, if so then I should read the input stream and check when it is prompted and only by that time I should pass is it? Please clarify me. – Lolly Feb 04 '13 at 12:03
1

One alternative to the answer provided above is to create a password file.

(e.g ~/.pgpass file (%APPDATA%\postgresql\pgpass.conf on Windows) formatted as:

hostname:port:database:username:password
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • This password is not PostgreSQL server password, this is password for private key which is totally different from PostgreSQL server's password. Guess you have misunderstood. – Lolly Feb 04 '13 at 13:46
1

Start by having a look at ProcessBuilder, it will make you life much easier....

ProcessBuilder pb = new ProcessBuilder("PostgreQL", "-D", "data/dir/" "start");
Process p = pb.start();

InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();

// Create a separate thread to read the input as needed...
// Create a seperate thread to deal with the output as needed...
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Don't forget to either [`redirectErrorStream()`](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#redirectErrorStream%28%29) or `// Create a seperate thread to deal with the error as needed...`. I am guessing that given `PostgreQL` (as opposed to `PostgreSQL`) we might be seeing some of the latter. ;) – Andrew Thompson Feb 01 '13 at 05:08
  • @AndrewThompson Why would you ever want to know when things go wrong? Or report the fact? ;) +1 nice point – MadProgrammer Feb 01 '13 at 05:17