简体   繁体   中英

Psql output csv file formatting

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?

Working script:

psql -h host -d test -U test -p 5432 -q --quiet --no-align --field-separator=';' --file=C:\Users\test\Documents\my_query.sql --output=C:\Users\test\Documents\res.csv

From PostgreSQL v12 on, 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.

Should work with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM