psql - save results of command to a file

后端 未结 10 1189
日久生厌
日久生厌 2020-12-12 09:10

I\'m using psql\'s \\dt to list all tables in a database and I need to save the results.

What is the syntax to export the results of a psql command to a

10条回答
  •  死守一世寂寞
    2020-12-12 09:53

    If you got the following error

    ufgtoolspg=> COPY (SELECT foo, bar FROM baz) TO '/tmp/query.csv' (format csv, delimiter ';');
    ERROR:  must be superuser to COPY to or from a file
    HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
    

    you can run it in this way:

    psql somepsqllink_or_credentials -c "COPY (SELECT foo, bar FROM baz) TO STDOUT (format csv, delimiter ';')"  > baz.csv
    

提交回复
热议问题