112

How do you hide the column names and row count in the output from psql?

I'm running a SQL query via psql with:

psql --user=myuser -d mydb --output=result.txt -c "SELECT * FROM mytable;"

and I'm expecting output like:

1,abc
2,def
3,xyz

but instead I get:

id,text
-------
1,abc
2,def
3,xyz
(3 rows)

Of course, it's not impossible to filter the top two rows and bottom row out after the fact, but it there a way to do it with only psql? Reading over its manpage, I see options for controlling the field delimiter, but nothing for hiding extraneous output.

1
  • 2
    psql -U username -X -A -w -t -c "select count(*) from yourtable)" produces just the output: 1 Commented Apr 24, 2021 at 15:23

3 Answers 3

149

You can use the -t or --tuples-only option:

psql --user=myuser -d mydb --output=result.txt -t -c "SELECT * FROM mytable;"

Edited (more than a year later) to add:

You also might want to check out the COPY command. I no longer have any PostgreSQL instances handy to test with, but I think you can write something along these lines:

psql --user=myuser -d mydb -c "COPY mytable TO 'result.txt' DELIMITER ','"

(except that result.txt will need to be an absolute path). The COPY command also supports a more-intelligent CSV format; see its documentation.

8
  • 30
    alternatively, if you want the header but not the row-count footer, run psql with --pset="footer=off" Commented Mar 21, 2013 at 14:36
  • 4
    COPY can indeed be a valid alternative, but the file will end up on the server, and not on the machine where psql is run...
    – fvu
    Commented Sep 1, 2015 at 14:23
  • @fvu: Good point. I usually ran psql on the server, so for me that wasn't an issue . . .
    – ruakh
    Commented Sep 1, 2015 at 15:36
  • 3
    I think @GabrielBurt comment deserves to be a standalone answer. That's exactly what I was looking for to generate a csv with header but without the pesky "(nnnn rows)" footer.
    – Pierre D
    Commented Sep 26, 2016 at 23:14
  • 1
    @Merlin: No, his comment is not the correct answer to the OP's question. A lot of people have found it useful, and that's great, but let's not pretend it's something it's not.
    – ruakh
    Commented May 13, 2018 at 22:16
33

You can also redirect output from within psql and use the same option. Use \o to set the output file, and \t to output tuples only (or \pset to turn off just the rowcount "footer").

\o /home/flynn/queryout.txt
\t on
SELECT * FROM a_table;
\t off
\o

Alternatively,

\o /home/flynn/queryout.txt
\pset footer off
. . .
1
  • 2
    When dumping multi line values I am still getting the + character at the end of each line, is there a way to stop this too?
    – Sodved
    Commented Jan 23 at 3:04
10

usually when you want to parse the psql generated output you would want to set the -A and -F ...

    # generate t.col1, t.col2, t.col3 ...
    while read -r c; do test -z "$c" || echo  , $table_name.$c  | \
       perl -ne 's/\n//gm;print' ; \
       done < <(cat << EOF | PGPASSWORD=${postgres_db_useradmin_pw:-} \
       psql -A -F  -v -q -t -X -w -U \
       ${postgres_db_useradmin:-} --port $postgres_db_port --host $postgres_db_host -d \
       $postgres_db_name -v table_name=${table_name:-}
    SELECT column_name
    FROM information_schema.columns
    WHERE 1=1
    AND table_schema = 'public'
    AND table_name   =:'table_name'  ;
    EOF
    )
    echo -e "\n\n"

You could find example of the full bash call here:

1
  • 3
    Well done for actually answering the question and winning the "Yet again the best answer is at the bottom of the page" prize.
    – Dino Dini
    Commented Sep 10, 2022 at 21:59

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