I'm using ProcessBuilder to run postgresql psql
command.
But I can't give it password at same line and I have to pass parameters and then it will prompt to enter password.
How can I pass this interactive parameters using ProcessBuilder?

- 30,799
- 15
- 56
- 79

- 1,205
- 3
- 18
- 44
-
1try this :-http://stackoverflow.com/questions/3468987/executing-another-application-from-java – Divya Mar 19 '14 at 09:47
-
2When you call `yourProcessBuilder.start()`, it will return a `Process` object. `Process` has `getOutputStream()` and `getInputStream()` methods, you can use them to read/write to the process – BackSlash Mar 19 '14 at 09:47
-
@BackSlash Could you please explain a bit more ? – Obtice Mar 19 '14 at 11:37
1 Answers
The title of the question indicates a general question about ProcessBuilder, but since the OP specifically mentions psql
, there are specific solutions that work better and don't require you to read the process output and respond to it.
However, the problem that you need to keep in mind is that passing passwords on the command line or in the environment variables is completely insecure because they can be spotted (at least on Unix) by using the ps
command.
Solution 1:
There is an option to pass the password in the PGPASSWORD
environment variable but this is not recommended as other users of the machine can see it.
Solution 2: (recommended)
There is a more secure option though, and that is to create a .pgpass
or pgpass.conf
file containing the password in the account of the user under which you are going to run the psql
program.
The file should contain lines in this format:
hostname:port:database:username:password
I've added links in the text above to the PostgreSQL documentation for more details.
You can also tell psql
where this pgpass.conf
file is on your disk using an environment variable PGPASSFILE
, and you can write this file from Java before invoking psql
.
This snippet should give you an idea how to do that with ProcessBuilder
:
String hostname = "...";
int port = 5432;
String database = "...";
String username = "...";
String password = "...";
File file = File.createTempFile("pgpass", "conf");
// TODO: ensure file permissions are correct
try (Writer w = new FileWriter(file)) {
w.write(String.format("%s:%d:%s:%s:%s\n", hostname, port, database, username, password));
}
ProcessBuilder processBuilder = new ProcessBuilder("psql"); // add other options to psql to the argument list
processBuilder.environment().put("PGPASSFILE", file.getAbsolutePath());
processBuilder.start();

- 30,799
- 15
- 56
- 79
-
Thanks for your answer but we want to develope an application that runs in 50 or more clients and we don't know how is their postgresql environment. We will get password and other arguments as parameter from user. – Obtice Mar 19 '14 at 11:15
-
-
And a second question: why do you need to start `psql` specifically? You can use the postgresql JDBC driver to execute commands against the database too. – Erwin Bolwidt Mar 19 '14 at 11:18
-
-
1I've added an idea and a code snippet you can use to pass the password to `psql` using a pgpass.conf file that you write out from Java, and then pass to `psql` using the environment variable `PGPASSFILE`. – Erwin Bolwidt Mar 19 '14 at 13:21