简体   繁体   中英

How to use ProcessBuilder for interactive commands?

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM