Save PL/pgSQL output from PostgreSQL to a CSV file

后端 未结 18 2290
名媛妹妹
名媛妹妹 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条回答
  •  旧时难觅i
    2020-11-22 12:11

    In terminal (while connected to the db) set output to the cvs file

    1) Set field seperator to ',':

    \f ','
    

    2) Set output format unaligned:

    \a
    

    3) Show only tuples:

    \t
    

    4) Set output:

    \o '/tmp/yourOutputFile.csv'
    

    5) Execute your query:

    :select * from YOUR_TABLE
    

    6) Output:

    \o
    

    You will then be able to find your csv file in this location:

    cd /tmp
    

    Copy it using the scp command or edit using nano:

    nano /tmp/yourOutputFile.csv
    

提交回复
热议问题