问题
im using the following code to Restore PostgreSQL database using java
Runtime r = Runtime.getRuntime();
Process p;
String cmd ="D:/Program Files/PostgreSQL/9.1/bin/pg_restore.exe --host localhost --port 5432 --username postgres --dbname mytestqq --role postgres --no-password --verbose D:\sathish\rawDatabase.backup";
p = r.exec(cmd);
i have 42 tables in the rawDatabase.backup file but only one table is getting restored why the rest of the tables are not happening whats wrong in my code? thanks in advance!!!!
回答1:
It's surprising that the command you show works at all, since you're failing to quote the spaces in the command path. Try:
String[] cmd = {
"D:\\Program Files\\PostgreSQL\\9.1\\bin\\pg_restore.exe",
"--host", "localhost",
"--port", "5432",
"--username", "postgres",
"--dbname", "mytestqq",
"--role", "postgres",
"--no-password",
"--verbose",
"D:\\sathish\\rawDatabase.backup"
};
p = r.exec(cmd);
Changes:
- Convert the single-string form to the much safer arguments array form of the
execcall; - Double the backslashes in the
rawDatabasepath since your original command fails to escape backslashes, so\ris a carriage return in the string instead of the\char followed by therchar. - Switch to doubled backslashes instead of forward slashes on the program path for consistency. This change probably doesn't matter.
Also check the return status of the process. You must use Process.waitFor() then once it has exited use Process.exitValue() to determine the result. You should examine the stderr and stdout captured by the Process object for errors and logging information.
The reason your program continues not to work is probably because:
- You have old
pg_restoreprocesses hanging around holding locks; and/or - You aren't consuming stdout and stderr so
pg_restoreruns out of buffered pipe space and blocks writing on the output stream.
This will all be much simpler if you use ProcessBuilder instead. ProcessBuilder lets you provide file streams to write output to and generally takes care of a lot of this for you. You must still wait for the process to terminate and check its return code though.
回答2:
Finally I got the solution
Runtime r = Runtime.getRuntime();
Process p;
ProcessBuilder pb;
r = Runtime.getRuntime();
pb = new ProcessBuilder(
"D:\\Program Files\\PostgreSQL\\9.1\\bin\\pg_restore.exe",
"--host=localhost",
"--port=5432",
"--username=postgres",
"--dbname=mytestqq",
"--role=postgres",
"--no-password",
"--verbose",
"D:\\sathish\\rawDatabase.backup");
pb.redirectErrorStream(true);
p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String ll;
while ((ll = br.readLine()) != null) {
System.out.println(ll);
}
回答3:
The following code perfectly work in taking the postgres DB dump using JAVA code..Try this
List<String> cmds = new ArrayList<String>();
cmds.add("C:\\Program Files\\PostgreSQL\\9.1\\bin\\pg_dump.exe");
cmds.add("-i");
cmds.add("-h");
cmds.add("localhost");
cmds.add("-p");
cmds.add("5432");
cmds.add("-U");
cmds.add("YOUR PG USERNAME");
cmds.add("-F");
cmds.add("c");
cmds.add("-b");
cmds.add("-v");
cmds.add("-f");
cmds.add("\"E:\\pg_dump.backup\"");//Location to store db Dump backup
cmds.add("lmd");
ProcessBuilder process = new ProcessBuilder();
process.command(cmds).start();
来源:https://stackoverflow.com/questions/15743529/restore-postgresql-database-using-java