2016-05-02 22 views
2

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

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()); 
} 
+1

但是你为什么在这里使用java? – e4c5

+0

进程/应用程序是否具有对文件位置@Abdelwahhab的写入权限?例如,进程的用户标识是什么,用户标识是否可以访问输出文件? – pnorton

+0

@pnorton是的,进程可以访问输出文件,该文件被正确创建,但没有内容被转储到它。我认为问题是使用postgres服务器密码。 – Wolverine219

回答

0

对于psql, “长选项”,需要一个等号:=。所以它需要例如--host=localhost。对于您需要将这些参数作为一个字符串参数传递给的ProcessBuilder:

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

你也应该使用ProcessBuilder.getErrorStream()看到来自psql任何错误信息捕捉的ProcessBuilder的错误输出。你可能想捕捉的常规输出,以及(使用getInputStream()


编辑

你得到的错误信息:

fe_sendauth:没有密码提供

表示您必须提供密码。

您可以通过将connection URL传递给pg_dump来完成此操作。

pb = new ProcessBuilder(
     "C:\\Program Files\\PostgreSQL\\9.3\\bin\\pg_dumpall.exe", 
     "--dbname=postgres://postgres:[email protected]/postgres", 
     "--format=custom", 
     "--blobs", 
     "--verbose", "--file=D:\\service_station_backup.backup", "service_station"); 
+0

嗨,感谢以下,我试过了你的建议,但仍然得到相同的结果,并从流程ErrorStream相同的消息:“fe_sendauth:没有密码提供”。 – Wolverine219

+0

然后你需要提供一个密码。通过设置'PGPASSWORD'环境变量,通过在'pgpass.conf'中配置它或者通过提供一个连接URL来实现。有关详细信息,请参阅手册:http://www.postgresql.org/docs/9.3/static/libpq-connect.html#AEN39257 –

2

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

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