4 ms·
With regards to psql2csv: The default psql can already do this very nicely. Just use \copy. \copy (select whatever from whatever) to 'yourlocalfile.csv' with (
by pilif 11y ago
With regards to psql2csv: The default psql can already do this very nicely. Just use \copy.
\copy (select whatever from whatever) to 'yourlocalfile.csv' with (format 'csv')
and if you want column headers, add a "header true" inside of the with clause.
Generally, \copy works just like COPY[1] but it does so from the remote server to the local machine, whereas file names given to COPY are relative to the server.
Yes. A dedicated tool might feel easier initially, but once you know how \copy works, you can always get a CSV file from whatever database you're connected to and no matter what machine you're on.
[1]: http://www.postgresql.org/docs/current/static/sql-copy.html http://www.postgresql.org/docs/current/static/sql-copy.html
- fphilipe 11y agopsql2csv is just a wrapper around what you're describing. The point is that you don't have to write the "boilerplate" when all you need is a single query. Plus, it works nice with stdin and stdout, making it handy for queries stored in a file, or for piping the CSV to another program.