Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm using PostgreSQL 9.1.X

I am trying to build psql script to print results without a header but including a footer.

http://www.postgresql.org/docs/9.1/static/app-psql.html

From the document above

\pset tuples_only

will turn both header and footer off. and

\pset footer off

will turn footer off only.

Is there a way in psql to turn the header off and keep the footer on?

I know there are many ways to work around this issue using shell/perl/whatever text tool you like, however I am wondering why there is a config for the footer but not one for the header?

id <--this line I don't want
---- <-- this line I don't want either
 1  <-- this line, yes
(1 row) <-- yes, I want it!
share|improve this question
    
Are you on Unix? If so, you can use head/tail/grep/awk/sed/a million other things –  FreshPhilOfSO Sep 12 '12 at 20:42
add comment

1 Answer

My solution is not quite turning off but rather discarding headers.

You can try to tail the query output:

\o | tail -n +2

With \o, you can redirect output to a file or a pipe, like in this case. This solution has its flaw, too: at least in my case, after execution of SELECT [...], I don't get back to a prompt unless I press a key. And the first output row appears after a prompt. If you then redirect output to a file, it shouldn't be a problem though.

This behaviour can be avoided if you set the PAGER environmental variable appropriately and always use pager is psql:

$ export PAGER='tail -n +3'

$ psql -U postgres -d test
psql (9.1.4, server 9.1.5)

test=# \pset pager always
Pager is always used.

test=# select * from a;
  2 | b
  3 | b
(2 rows)

In the psql version that comes with PostgreSQL 9.2 you can use the \setenv command for convenience (I mean that you don't have to set an env variable which may affect other applications as well).

share|improve this answer
    
nice hack, good to know pager option. but really there is no option for header itself? Maybe I should check postgresql's mailist –  skong Sep 14 '12 at 15:20
    
@sinbadblue No. As you've already checked, no. You can post a feature request, though :) Would be a useful advancement. –  dezso Sep 14 '12 at 19:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.