views:

38

answers:

3

Hello All!

Quick question (I hope!): if I use \i to feed an input file into psql, can I get the output from the queries to be saved into a file? If so, how? Thanks!!

+2  A: 

Based on the documentation, the \o is for directing output to a file.

OMG Ponies
This doesn't show the \echo that are in my input file but it does grab everything else. Maybe someone else knows why, but this is good enough for now. I should've figured \o would be the way to do it! Thanks again.
Airjoe
The [solution I proposed](http://stackoverflow.com/questions/3939329/putting-output-from-postgres-i-input-to-a-file/3939359#3939359) will include `\echo` output :-).
Matt Solnit
+2  A: 

Sure can:

dbname=> \o /home/outputfile.csv
dbname=> select * from table;
dbname=> \q

Output will be streamed to the file.

Tim
+2  A: 

Using \o, as recommended by others, is a good solution. Just for fun, though, another way to do this would be to pipe the input file to psql from the command line, rather than using the \i command. Then you could re-direct the output to another file. For example:

psql < input.sql > output.txt

This has some interesting side effects. For example, if you have timing turned on (\timing on), then using \o would not cause the timing results to be piped to the output file, but re-direction would. Same thing with \echo statements.

Matt Solnit