Home > Back-end >  Psql output csv file formatting
Psql output csv file formatting

Time:10-06

Trying to make batch file that will get query from script file and put results into csv. Batch file code:

psql -h host -d test -U test -p 5432 -a -q -f C:\Users\test\Documents\my_query.sql TO STDOUT WITH CSV HEADER DELIMITER ';' > C:\Users\test\Documents\res.csv

In result file I'm getting result like this:

select *

from public.test

limit 3

    id    |    name     | count_01
---------- ------------ --------------- 
 11021555 | a       |             1 |
 39534568 | b       |             2 |
 11695210 | c       |             3 |

(3 rows)

How to get only script results without rows count and symbols like '|' or ' ' and using ';' delimetres as in the usual csv file?

CodePudding user response:

Should work with

psql -h host -d dbname -U user -p port -a -q -f my_query.sql -o res.csv --record-separator=',' --csv

CodePudding user response:

You can use the CSV output format of psql:

psql --quiet --csv --file=my_query.sql --output=res.csv

--quiet suppresses the psql welcome message.

  • Related