what is the easiest way to save PL/pgSQL output from a PostgreSQL database to a csv file? I'm using PostgreSQL 8.4 with pgAdmin III and psql plugin where I run queries from.
thanks!
martin
what is the easiest way to save PL/pgSQL output from a PostgreSQL database to a csv file? I'm using PostgreSQL 8.4 with pgAdmin III and psql plugin where I run queries from. thanks! martin |
||||
|
If you want something easy to re-use or automate, you can use Postgresql's built in COPY command. e.g.
Note that this approach runs entirely on the remote server (it can't write to your local PC!), and requires you to be connected to Postgres as a "superuser" in order to write files to that machine's local filesystem. If you are connecting using the
From the docs:
|
|||||||||||||
|
There are several solutions: 1
|
In terminal (while connected to the db) set output to the cvs file 1) set field seperator to ',' by:
2) set output format unaligned:
3) show only tuples
4) set output
5) execute your query
6) output
you will be able to fin your cvs file in the
or edit using nano:
|
|||||||
|
See |
|||||
|
If you're interested in all the columns of a particular table along with headers, you can use
This is a tiny bit simpler than
which, to the best of my knowledge, are equivalent. |
|||
|
In pgAdmin III there is an option to export to file from the query window. In the main menu it's Query -> Execute to file or there's a button that does the same thing (it's a green triangle with a blue floppy disk as opposed to the plain green triangle which just runs the query). If you're not running the query from the query window then I'd do what IMSoP suggested and use the copy command. |
|||
|
I don't have pgadmin in front of me but working from memory it's just a matter of highlighting all or part of the result set you want, right clicking and selecting export. I'm pretty sure the export has a .csv option. |
|||
|