How to save PostgreSql query output to file

The PostgreSql query should be be terminated with a semicolon for execution. We can use a '\g' also as an alternative.

In-order to write the result of the query to a file, include the file name after '\g'.

Sample query is as follows.

> select 1, 2, 3 \g result.txt

Here the output of the query will be save to the file with name 'result.txt' within the current working directory.

That's all…

Leave a Reply

Your email address will not be published. Required fields are marked *