使用 java 和密码进行 Postgresql 备份和恢复
Postgresql Backup and restore using java with password
我想使用 pg_dump 备份和恢复 postgresql 数据库。
问题是:
当我执行此命令时在命令行中:
pg_dump -h localhost -p 5432 -U postgres -F t -f test2.tar myDatabase
我收到此提示消息以提供数据库密码:
Mot de passe :
所以,它有效
但是,在 java 的上下文中:
我只能做:
String cmd = "pg_dump -U postgres -h localhost -p 5342";
cmd += " -F t -f " + file.getCanonicalPath();
cmd += " myDatabase";
Process p = Runtime.getRuntime().exec(cmd);
int result= p.waitFor();
if (result == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("There is an error");
}
以上代码无效。那么,我可以做些什么来在运行时提供数据库密码?
来自关于 pg_dump 的 PostgreSQL 文档:
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.
那么您能否尝试在 pg_dump
命令中传递 -w
选项并提供 .pgpass
file 和密码?
我想使用 pg_dump 备份和恢复 postgresql 数据库。 问题是: 当我执行此命令时在命令行中:
pg_dump -h localhost -p 5432 -U postgres -F t -f test2.tar myDatabase
我收到此提示消息以提供数据库密码:
Mot de passe :
所以,它有效
但是,在 java 的上下文中: 我只能做:
String cmd = "pg_dump -U postgres -h localhost -p 5342";
cmd += " -F t -f " + file.getCanonicalPath();
cmd += " myDatabase";
Process p = Runtime.getRuntime().exec(cmd);
int result= p.waitFor();
if (result == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("There is an error");
}
以上代码无效。那么,我可以做些什么来在运行时提供数据库密码?
来自关于 pg_dump 的 PostgreSQL 文档:
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.
那么您能否尝试在 pg_dump
命令中传递 -w
选项并提供 .pgpass
file 和密码?