Thread: how to redirect output to a file

how to redirect output to a file

From
pc
Date:
Hi,

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


Re: how to redirect output to a file

From
"Usama Dar"
Date:


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

Re: how to redirect output to a file

From
"A. Kretschmer"
Date:
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

Re: how to redirect output to a file

From
Ron St-Pierre
Date:
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