Reputation: 153
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
Upvotes: 2
Views: 1304
Reputation: 246788
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.
Upvotes: 2
Reputation: 1868
Should work with
psql -h host -d dbname -U user -p port -a -q -f my_query.sql -o res.csv --record-separator=',' --csv
Upvotes: 0