Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using COPY command with SQL query

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hi,

I am trying to perform a copy using a query.

This works for example:

Code:
psql -h localhost -U admin suppliers -c 'COPY connections TO STDOUT WITH CSV' > /tmp/suppliers.csv

This on the other hand doesn't work:
Code:
psql -h localhost -U admin suppliers -c 'COPY (select id, latitude, longitude from connections order by id) TO STDOUT WITH CSV' > /tmp/suppliers.csv

Any ideas how to get this to work?
 
Hi

That is not possible. The [tt]copy[/tt] statement's parameter must be a table.

Probably your best choice is to create a temporary table :
Code:
psql -h localhost -U admin suppliers -c 'create table whatever as select id, latitude, longitude from connections order by id; COPY whatever TO STDOUT WITH CSV; drop table whatever' > /tmp/suppliers.csv
Or you can format abit the output, but you will have problem with text values containing newline ( \n ) characters :
Code:
psql -d suppliers -h localhost -U admin -t -A -F $'\t' -P null='\N' -c 'select id, latitude, longitude from connections order by id'

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top