Save PL/pgSQL output from PostgreSQL to a CSV file

后端 未结 18 2025
名媛妹妹
名媛妹妹 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:19

    CSV Export Unification

    This information isn't really well represented. As this is the second time I've needed to derive this, I'll put this here to remind myself if nothing else.

    Really the best way to do this (get CSV out of postgres) is to use the COPY ... TO STDOUT command. Though you don't want to do it the way shown in the answers here. The correct way to use the command is:

    COPY (select id, name from groups) TO STDOUT WITH CSV HEADER
    

    Remember just one command!

    It's great for use over ssh:

    $ ssh psqlserver.example.com 'psql -d mydb "COPY (select id, name from groups) TO STDOUT WITH CSV HEADER"' > groups.csv
    

    It's great for use inside docker over ssh:

    $ ssh pgserver.example.com 'docker exec -tu postgres postgres psql -d mydb -c "COPY groups TO STDOUT WITH CSV HEADER"' > groups.csv
    

    It's even great on the local machine:

    $ psql -d mydb -c 'COPY groups TO STDOUT WITH CSV HEADER' > groups.csv
    

    Or inside docker on the local machine?:

    docker exec -tu postgres postgres psql -d mydb -c 'COPY groups TO STDOUT WITH CSV HEADER' > groups.csv
    

    Or on a kubernetes cluster, in docker, over HTTPS??:

    kubectl exec -t postgres-2592991581-ws2td 'psql -d mydb -c "COPY groups TO STDOUT WITH CSV HEADER"' > groups.csv
    

    So versatile, much commas!

    Do you even?

    Yes I did, here are my notes:

    The COPYses

    Using /copy effectively executes file operations on whatever system the psql command is running on, as the user who is executing it1. If you connect to a remote server, it's simple to copy data files on the system executing psql to/from the remote server.

    COPY executes file operations on the server as the backend process user account (default postgres), file paths and permissions are checked and applied accordingly. If using TO STDOUT then file permissions checks are bypassed.

    Both of these options require subsequent file movement if psql is not executing on the system where you want the resultant CSV to ultimately reside. This is the most likely case, in my experience, when you mostly work with remote servers.

    It is more complex to configure something like a TCP/IP tunnel over ssh to a remote system for simple CSV output, but for other output formats (binary) it may be better to /copy over a tunneled connection, executing a local psql. In a similar vein, for large imports, moving the source file to the server and using COPY is probably the highest-performance option.

    PSQL Parameters

    With psql parameters you can format the output like CSV but there are downsides like having to remember to disable the pager and not getting headers:

    $ psql -P pager=off -d mydb -t -A -F',' -c 'select * from groups;'
    2,Technician,Test 2,,,t,,0,,                                                                                                                                                                   
    3,Truck,1,2017-10-02,,t,,0,,                                                                                                                                                                   
    4,Truck,2,2017-10-02,,t,,0,,
    

    Other Tools

    No, I just want to get CSV out of my server without compiling and/or installing a tool.

提交回复
热议问题