psql:警告:忽略额外的命令行参数“from”

Tho*_*ggi 9 postgresql bash shell

我正在尝试执行非交互式 postgres 命令。

PGPASSWORD=$PGPASS psql -h 127.0.0.1 -U postgresql -d $PGDB --command "select count(*) from services"
Run Code Online (Sandbox Code Playgroud)

这给了我这样的回应。

psql:警告:忽略额外的命令行参数“from”

psql:警告:额外的命令行参数“services;” 被忽略

psql:警告:忽略额外的命令行参数“mydbname”

psql:致命:数据库“count(*)”不存在

我读到这可能是因为终端 / bash 试图将每个参数分解为--command/-c作为它自己的参数。

我也尝试过这个:

PSQLARGS=(-h 127.0.0.1 -U postgresql -c )
PSQLARGS+=("select count(*) from services;")
PSQLARGS+=(${PGDB})
PGPASSWORD=$PGPASS psql "${PSQLARGS[@]}"
Run Code Online (Sandbox Code Playgroud)

某种强制终端知道它是一个参数的方法,这也不起作用。

kst*_*tis 7

这是一个非常便宜且常见的错误。将所有内容放在引号中应该可以解决问题。

尝试这个:

PGPASSWORD="$PGPASS" psql -h '127.0.0.1' -U 'postgresql' -d "$PGDB" --command "select count(*) from services"
Run Code Online (Sandbox Code Playgroud)

让我们知道它是否适合您。