gpt4 book ai didi

java - 如何使用 java 备份 postgres 数据库

转载 作者:行者123 更新时间:2023-11-29 11:55:08 25 4
gpt4 key购买 nike

这是我在 java 中使用 pg_dump 9.3 备份数据库的代码。我遇到的问题是结果文件总是空的,退出代码是 1,有什么想法吗?

public static void backupDb() throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
Process p;
ProcessBuilder pb;
rt = Runtime.getRuntime();
pb = new ProcessBuilder(
"C:\\Program Files\\PostgreSQL\\9.3\\bin\\pg_dumpall.exe",
"--host", "localhost",
"--port", "5432",
"--username", "postgres",
"--no-password",
"--format", "custom",
"--blobs",
"--verbose", "--file", "D:\\service_station_backup.backup", "service_station");
p = pb.start();
p.waitFor();
System.out.println(p.exitValue());
}

最佳答案

感谢大家的帮助,终于找到了完美的代码。

public static void exportDb2() throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
Process p;
ProcessBuilder pb;
rt = Runtime.getRuntime();
pb = new ProcessBuilder(
"C:\\Program Files\\PostgreSQL\\9.3\\bin\\pg_dump.exe",
"--host", "localhost",
"--port", "5432",
"--username", "postgres",
"--no-password",
"--format", "custom",
"--blobs",
"--verbose", "--file", "D:\\service_station_backup.backup", "service_station");
try {
final Map<String, String> env = pb.environment();
env.put("PGPASSWORD", "admin");
p = pb.start();
final BufferedReader r = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
String line = r.readLine();
while (line != null) {
System.err.println(line);
line = r.readLine();
}
r.close();
p.waitFor();
System.out.println(p.exitValue());

} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
}

关于java - 如何使用 java 备份 postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com