我在很多地方都看到过这个问题,也就是说,从PostgreSQL以编程方式进行备份的问题。我的解决方案是使用ExpectJ,但是我无法让我的代码正常工作。
我有以下代码:
public class BackupUtility
{
public static void main(String[] args)
{
try
{
ExpectJ exp = new ExpectJ(20);
Spawn shell = exp.spawn("\"C:\\Program Files\\PostgreSQL\\8.4\\bin\\pg_dump.exe\" " +
"-h localhost -U myUserName myDB");
shell.expect("Password: ");
shell.send("myPassword");
System.out.println(shell.getCurrentStandardOutContents());
shell.expectClose()
}
catch (Exception e)
{
e.printStackTrace();
}
}
}然而,在shell.expect线路上,它超时了。
当我从命令提示符运行此命令时,它看起来如下所示:
C:\Documents Settings\bobjonesthe3rd>"C:\Program Files\PostgreSQL\8.4\bin\pg_dump“-h本地主机-U myUserName myDB
密码:
所以,我知道它会提示输入密码,但由于某种原因,ExpectJ没有收到提示。
任何帮助都将不胜感激。
谢谢,马特
发布于 2010-07-23 13:05:07
我非常确定一种更简单的方法,不需要使用expect和send,就是通过.pgpass file。也在pg_dump docs中引用。
https://stackoverflow.com/questions/3313657
复制相似问题