psql - save results of command to a file

后端 未结 10 1200
日久生厌
日久生厌 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条回答
  •  旧时难觅i
    2020-12-12 09:45

    The psql \o command was already described by jhwist.

    An alternative approach is using the COPY TO command to write directly to a file on the server. This has the advantage that it's dumped in an easy-to-parse format of your choice -- rather than psql's tabulated format. It's also very easy to import to another table/database using COPY FROM.

    NB! This requires superuser privileges and will write to a file on the server.

    Example: COPY (SELECT foo, bar FROM baz) TO '/tmp/query.csv' (format csv, delimiter ';')

    Creates a CSV file with ';' as the field separator.

    As always, see the documentation for details

提交回复
热议问题