Alex
Alex

Reputation: 897

Making an empty output from psql

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.

Upvotes: 3

Views: 3086

Answers (2)

Daniel Vérité
Daniel Vérité

Reputation: 61516

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

Upvotes: 2

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656391

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.

Upvotes: 2

Related Questions