我可以使用此
成功备份和恢复cmdpg_dump -h localhost -p 5433 --username postgres --no-password -f C:\tmp\test.sql mydb
但是当我尝试通过Java运行相同的东西时,我得到了这个输出:
String[] cmdDump = {
"C:\\Program Files\\PostgreSQL\\9.3\\bin\\pg_dump.exe",
"--host", "localhost",
"--port", "5432",
"--username", "test",
"--no-password",
"--verbose",
"--file", "C:\\tmp\\test.sql",
"mydb"
};
Runtime rt = Runtime.getRuntime();
Process p;
p = rt.exec(cmdDump);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bufErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String ll;
p.waitFor();
int processComplete = p.exitValue();
if (processComplete == 0)
{
while ((ll = br.readLine()) != null) {
System.out.println(ll);
}
System.out.println("Backup created successfully");
}
else
{
while ((ll = bufErr.readLine()) != null) {
System.out.println(ll);
}
System.out.println("Could not create the backup");
}
输出:
pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Could not create the backup
不确定为什么从Eclipse中看不到可从cmd访问的数据库。
我的pgpass.conf
文件已为我的登录用户正确配置。