运行 java 程序中的 psql 命令
running psql command in a java program
我有一个使用 java 代码的要求,我需要 create/delete/alter postgres 表。
我写了一个程序如下:
public static void main(String[] args) throws IOException {
System.out.println("Hello World!");
Process p = Runtime.getRuntime().exec("psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql");
}
test.sql 文件如下所示,
Create TABLE MyTable1
(
VersionNumber VARCHAR(32) NOT NUll
);
Create TABLE MyTable2
(
VersionNumber VARCHAR(32) NOT NUll
);
Create TABLE MyTable3
(
VersionNumber VARCHAR(32) NOT NUll
);
问题:
如果我运行相同的psql命令:
psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql
在命令行中,它要求输入密码并创建表。
但是在java程序中,没有规定提供密码。请让我知道如何实现它。
首先,实际使用JDBC连接数据库和运行你的SQL语句会更好。如果您设置为使用命令行 psql,则可以使用 PGPASSWORD
环境变量来设置密码:
String command = "psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql";
String[] envVars = { "PGPASSWORD=yourpassword" };
Process p = Runtime.getRuntime().exec(command, envVars);
如有必要,您可以从 stdin
中读取密码。但是,同样,最好在 JDBC.
上执行此操作
您可以使用 connection URL 代替:
psql -f d:\test.sql postgresql://postgres:password@localhost:5433/testdb
关于 a_horse_with_no_name
并在 Windows OS 使用 Java 代码启动 psql
并使用 ProcessBuilder
以下轻微修改对我有用:
ProcessBuilder builder = new ProcessBuilder();
String connUrl = "postgresql://user:password@host:port/dbname";
String sqlFileToProcess = "--file=Disk:\path\to\file.sql";
builder.command("psql.exe", sqlFileToProcess, connUrl);
builder.directory(new File("Disk:\Path\to\psql\containing\folder"));
Process process = builder.start();
int exitCode = 0;
try {
exitCode = process.waitFor();
int len;
if ((len = process.getErrorStream().available()) > 0) {
byte[] buf = new byte[len];
process.getErrorStream().read(buf);
System.err.println("Command error:\t\""+new String(buf)+"\"");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
assert exitCode == 0;
不知道为什么,但如果:
-f Disk:\path\to\file.sql
作为 Java 中的参数 - 代码抛出:
Command error: "unrecognized win32 error code: 123 psql: error: Disk:/path/to/file.sql: Invalid argument
"
(注意otput和input中斜杠和反斜杠的重定向)
我有一个使用 java 代码的要求,我需要 create/delete/alter postgres 表。
我写了一个程序如下:
public static void main(String[] args) throws IOException {
System.out.println("Hello World!");
Process p = Runtime.getRuntime().exec("psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql");
}
test.sql 文件如下所示,
Create TABLE MyTable1
(
VersionNumber VARCHAR(32) NOT NUll
);
Create TABLE MyTable2
(
VersionNumber VARCHAR(32) NOT NUll
);
Create TABLE MyTable3
(
VersionNumber VARCHAR(32) NOT NUll
);
问题:
如果我运行相同的psql命令:
psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql
在命令行中,它要求输入密码并创建表。
但是在java程序中,没有规定提供密码。请让我知道如何实现它。
首先,实际使用JDBC连接数据库和运行你的SQL语句会更好。如果您设置为使用命令行 psql,则可以使用 PGPASSWORD
环境变量来设置密码:
String command = "psql -U postgres -d testdb -h localhost -p 5433 -f D:\test.sql";
String[] envVars = { "PGPASSWORD=yourpassword" };
Process p = Runtime.getRuntime().exec(command, envVars);
如有必要,您可以从 stdin
中读取密码。但是,同样,最好在 JDBC.
您可以使用 connection URL 代替:
psql -f d:\test.sql postgresql://postgres:password@localhost:5433/testdb
关于 a_horse_with_no_name
ProcessBuilder builder = new ProcessBuilder();
String connUrl = "postgresql://user:password@host:port/dbname";
String sqlFileToProcess = "--file=Disk:\path\to\file.sql";
builder.command("psql.exe", sqlFileToProcess, connUrl);
builder.directory(new File("Disk:\Path\to\psql\containing\folder"));
Process process = builder.start();
int exitCode = 0;
try {
exitCode = process.waitFor();
int len;
if ((len = process.getErrorStream().available()) > 0) {
byte[] buf = new byte[len];
process.getErrorStream().read(buf);
System.err.println("Command error:\t\""+new String(buf)+"\"");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
assert exitCode == 0;
不知道为什么,但如果:
-f Disk:\path\to\file.sql
作为 Java 中的参数 - 代码抛出:
Command error: "unrecognized win32 error code: 123 psql: error: Disk:/path/to/file.sql: Invalid argument
"
(注意otput和input中斜杠和反斜杠的重定向)