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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

export into csv

Status
Not open for further replies.

andnos

Technical User
Nov 21, 2005
48
US
In MYSQL there's a functionality where I can export a select statement directly to a file... such as...

SELECT * FROM table
INTO OUTFILE '/tmp/testing.txt'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

is there something similar to this in PostGreSQL?
 
Code:
  \h COPY
Anweisung:    COPY
Beschreibung: kopiert Daten zwischen einer Datei und einer Tabelle
Syntax:
(...from-syntax...)

COPY tabellenname [ ( spalte [, ...] ) ]
    TO { 'dateiname' | STDOUT }
    [ [ WITH ]
          [ BINARY ]
          [ OIDS ]
          [ DELIMITER [ AS ] 'trennzeichen' ]
          [ NULL [ AS ] 'null-zeichenkette' ]
          [ CSV [ HEADER ]
                [ QUOTE [ AS ] 'quote' ]
                [ ESCAPE [ AS ] 'escape' ]
                [ FORCE QUOTE spalte [, ...] ]
maybe \h COPY in psql will show you a similar message in your language.

don't visit my homepage:
 
You could do this:

SELECT * FROM table \g /tmp/testing

or

\o /tmp/testing
select * from table;

Both when send output to file.
Hope this helps
TT

 
test=# COPY (SELECT count(*) FROM pg_class) TO '/tmp/file.csv' CSV;
COPY 1

something like this is even more fancy.
can you export the result of any query.

we implemented this feature for PostgreSQL 8.2 and beyond.

best regards,

hans

Hans-Jürgen Schönig
Cybertec Schönig & Schönig GmbH
URL: PostgreSQL Support, Training, Replication
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top