How do you print the result of a PostgreSQL query in CSV or TSV format from the command line?

后端 未结 8 1623
庸人自扰
庸人自扰 2021-01-30 20:05

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

8条回答
  •  醉话见心
    2021-01-30 20:23

    Starting from Bohemian's answer, I found these flags useful:

    psql my_database -U myuser -A -F , -X -t -f /path/to/query.sql -o /path/to/output.csv
    
    • Unaligned output mode: -A
    • Use comma as field delimiter: -F ,
    • Do not read psqlrc: -X
    • Tuples only (no header/footer): -t
    • File containing SQL query: -f
    • Output file: -o

提交回复
热议问题