how to redirect output to a file

Started by pcover 18 years ago4 messagesgeneral
Jump to latest
#1pc
chavanpriya@gmail.com

Hi,

How to redirect the output of an sql command to a file?
Thanks in advance

#2Usama Dar
munir.usama@gmail.com
In reply to: pc (#1)
Re: how to redirect output to a file

On Dec 5, 2007 9:19 AM, pc <chavanpriya@gmail.com> wrote:

Hi,

How to redirect the output of an sql command to a file?
Thanks in advance

if you are using psql

postgres=# \o ~/sql.out
postgres=# select * from foo;

the output will be directed to a file, when you need to stop doing it

postgres=# \o

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

--
Usama Munir Dar http://linkedin.com/in/usamadar
Consultant Architect
Cell:+92 321 5020666
Skype: usamadar

#3A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: pc (#1)
Re: how to redirect output to a file

am Tue, dem 04.12.2007, um 20:19:29 -0800 mailte pc folgendes:

Hi,

How to redirect the output of an sql command to a file?
Thanks in advance

within psql you can use \o <filename>, from the shell you can use this:

kretschmer@pegasus:~$ echo "select now()" | psql test > now.txt
kretschmer@pegasus:~$ cat now.txt
now
-------------------------------
2007-12-06 14:21:58.963405+01
(1 row)

Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

#4Ron St-Pierre
ron.pgsql@shaw.ca
In reply to: A. Kretschmer (#3)
Re: how to redirect output to a file

A. Kretschmer wrote:

am Tue, dem 04.12.2007, um 20:19:29 -0800 mailte pc folgendes:

Hi,

How to redirect the output of an sql command to a file?
Thanks in advance

within psql you can use \o <filename>, from the shell you can use this:

kretschmer@pegasus:~$ echo "select now()" | psql test > now.txt
kretschmer@pegasus:~$ cat now.txt
now
-------------------------------
2007-12-06 14:21:58.963405+01
(1 row)

Regards, Andreas

This is similar to Andreas' solution, and which we use in our shell scripts:

postgres@arya ~]$ psql mydb -c "SELECT cola, colb, description FROM
myfile;" > myOutFile.txt

If the sql string contains multiple commands, they will be executed
within a single transaction, unless you use BEGIN/COMMIT within it to
split it up into multiple transactions.

Ron