Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting output from Postgres \i input to a file?

Tags:

sql

postgresql

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

like image 583
Joseph Avatar asked Oct 15 '10 03:10

Joseph


People also ask

How do I spool in PostgreSQL?

PostgreSQL doesn't have a SPOOL command. You can direct output to a file using COPY (as you've discovered) or by using psql. Psql is a command-line tool. It's not a re-implementation of Oracle's SPOOL.

How do I store a file in PostgreSQL?

PostgreSQL provides two distinct ways to store binary data. Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.


2 Answers

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

like image 63
OMG Ponies Avatar answered Oct 06 '22 11:10

OMG Ponies


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.

like image 37
Matt Solnit Avatar answered Oct 06 '22 10:10

Matt Solnit