Save PL/pgSQL output from PostgreSQL to a CSV file

后端 未结 18 2294
名媛妹妹
名媛妹妹 2020-11-22 11:56

What is the easiest way to save PL/pgSQL output from a PostgreSQL database to a CSV file?

I\'m using PostgreSQL 8.4 with pgAdmin III and PSQL plugin where I run que

18条回答
  •  执笔经年
    2020-11-22 12:03

    New version - psql 12 - will support --csv.

    psql - devel

    --csv

    Switches to CSV (Comma-Separated Values) output mode. This is equivalent to \pset format csv.


    csv_fieldsep

    Specifies the field separator to be used in CSV output format. If the separator character appears in a field's value, that field is output within double quotes, following standard CSV rules. The default is a comma.

    Usage:

    psql -c "SELECT * FROM pg_catalog.pg_tables" --csv  postgres
    
    psql -c "SELECT * FROM pg_catalog.pg_tables" --csv -P csv_fieldsep='^'  postgres
    
    psql -c "SELECT * FROM pg_catalog.pg_tables" --csv  postgres > output.csv
    

提交回复
热议问题