I\'d like to execute a query from the shell (not in the interactive psql client) and have it print the CSV or TSV representation of the output to STDOUT. How do you do that with
If you are using PostgreSQL 8.2 or newer, use this for CSV:
psql -c "COPY (
and this of TSV, with proper NULLs:
psql -c "COPY (
The CSV form will properly quote any fields that contain the double-quote character. See the PostgreSQL documentation of your specific version for more details and options for COPY.