2016-12-29 122 views
2

我试图执行PostgreSQL中几个命令,我通过命令行支付PSQL执行命令,但它返回仅从结果:第二个:无法从终端

psql -d data1 -U user123 -c "select count(*) from table1; select count(*) from table2;" 

而当我登录到“psql”并运行相同的命令时,我得到2个结果集。

它为什么不起作用?

回答

1

psql --help给(部分)

-c, --command=COMMAND run only single command (SQL or internal) and exit 

你有两个命令这里,得到两个结果,你需要combine the queries(一UNION);像

psql -d data1 -U user123 -c \ 
    "select count(*) from table1 UNION ALL select count(*) from table2;" 

或者,运行两个命令(查询)

psql -d data1 -U user123 -c "select count(*) from table1;" 
psql -d data1 -U user123 -c "select count(*) from table2;" 

或者,你可以把两个命令在一个文件中,并使用-fpsql --help说)

-f, --file=FILENAME  execute commands from file, then exit 
+0

在我的问题中有什么不明确的地方? – Karim

+0

@Karim编辑。我希望更清楚。 –

+0

重新阅读我的问题。 – Karim

0

使用管道:

$ echo " 
> select 1; 
> \d 
> " | psql # here is the magic, happy New Year *~<:o) 
╔══════════╗ 
║ ?column? ║ 
╠══════════╣ 
║  1 ║ 
╚══════════╝ 
(1 row) 

       List of relations 
╔════════╤════════════════════╤══════╤══════════╗ 
║ Schema │  Name  │ Type │ Owner ║ 
╠════════╪════════════════════╪══════╪══════════╣ 
║ public │ dummy    │ view │ postgres ║ 
║ public │ pg_stat_statements │ view │ nd  ║ 
╚════════╧════════════════════╧══════╧══════════╝