gpt4 book ai didi

java - 使用java创建数据库备份postgresql

转载 作者:行者123 更新时间:2023-12-01 10:50:30 26 4
gpt4 key购买 nike

我尝试使用 Java 在 posgresql 中备份数据库,但是当我运行程序时,出现此错误:

Exception in thread "main" java.io.IOException: Cannot run program "pg_dump": CreateProcess error=2, specified file not found

这是我的代码

      String path = "C:\\Program Files (x86)\\PostgreSQL\\9.1\\bin\\pg_dump.exe";
Runtime r = Runtime.getRuntime();

//PostgreSQL variables
String host = "localhost";
String user = "postgres";
String dbase = "GestionPharmacie";
String password = "*****";
Process p;
ProcessBuilder pb;


r = Runtime.getRuntime();
pb = new ProcessBuilder("pg_dump", "-v", "-h", host, "-f", path, "-U", user, dbase);
pb.environment().put("PGPASSWORD", password);
pb.redirectErrorStream(true);
p = pb.start();

最佳答案

-D是一个无效选项,我设置了一个主机属性。

{
String path = "D:\\aaa";
Runtime r = Runtime.getRuntime();

//PostgreSQL variables
String host = "localhost";
String user = "postgres";
String dbase = "GestionPharmacie";
String password = "*****";
Process p;
ProcessBuilder pb;


r = Runtime.getRuntime();
pb = new ProcessBuilder("pg_dump", "-v", "-h", host, "-f", path, "-U", user, dbase);
pb.environment().put("PGPASSWORD", password);
pb.redirectErrorStream(true);
p = pb.start();

} catch (Exception e) {

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

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