6

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!!

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

8

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

2 Comments

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.
The solution I proposed will include \echo output :-).
7

Sure can:

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

Output will be streamed to the file. Or

dbname=> \o

To stop streaming to file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.