Making an empty output from psql

有些话、适合烂在心里 提交于 2021-02-09 11:55:14

问题


I am running a psql command that executes a complex query. There's nothing that query produces, as such, psql returns "(No rows)" in the output.

Is there a way to make psql to return an empty string?

I've tried using --pset=tuples-only=on and --pset=footer=off and -q in all variations, and it doesn't seem to work.

Footer option works while in psql shell prompt, but doesn't work from script.

Tried on 9.1.7, need this for 8.4, 9.1 and 9.2.


回答1:


May be good enough:

$ psql -Axt -c 'select 1 where 1=0'

produces an empty string

EDIT following comments: The command above produces an empty line, so that includes and end-of-line.

To produce nothing at all, remove the -x option.

Not that it would make any difference to the shell anyway, as shown below:

with -x:

r=`psql -Atx -d test -c "select 1 where 1=0"`  
echo $r | od -c  
0000000  \n  
0000001  

without -x:

r=`psql -At -d test -c "select 1 where 1=0"`
echo $r | od -c
0000000  \n
0000001



回答2:


Is there a way to make psql to return an empty string?

You can simply append SELECT '' after the first query. Ex.:

psql -Atp5432 mydb -c "SELECT 1 WHERE FALSE; SELECT ''"

Replace SELECT 1 WHERE FALSE with your complex query that doesn't return a row.



来源:https://stackoverflow.com/questions/14383146/making-an-empty-output-from-psql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!