PostgreSQL

Write output into a file

Source

$ psql
Welcome to psql 8.3.6, the PostgreSQL interactive terminal

postgres=#>
postgres=#>\c db
db=>\o /tmp/outputfile.txt
db=>\dt
db=>SELECT * FROM ....

# Reset output
db=>\o