Zet - How do I save output from psql to a file?

How do I save output from psql to a file?

To save the output from a psql query to a file, you can use the \o meta-command. Here’s how you can do it:

  1. Run your psql query as usual.
  2. Before running the query, use the \o meta-command followed by the file path where you want to save the output.

For example, if you want to save the output to a file named output.txt, you would do the following:

\o output.txt
SELECT * FROM your_table;

This will redirect the output of your query to the specified file. To stop saving output to a file and return to displaying on screen, simply use \o without any arguments:

\o

#psql #postgres