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.
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. |
|||||
|
Do you want the resulting file on the server, or on the client? Server sideIf you want something easy to re-use or automate, you can use Postgresql's built in COPY command. e.g.
This approach runs entirely on the remote server - it can't write to your local PC. It also needs to be run as a Postgres "superuser" (normally called "root") because Postgres can't stop it doing nasty things with that machine's local filesystem. That doesn't actually mean you have to be connected as a superuser (automating that would be a security risk of a different kind), because you can use the The crucial part is that your function is there to perform additional checks, not just by-pass the security - so you could write a function which exports the exact data you need, or you could write something which can accept various options as long as they meet a strict whitelist. You need to check two things:
I've written a blog post expanding on this approach, including some examples of functions that export (or import) files and tables meeting strict conditions. Client sideThe other approach is to do the file handling on the client side, i.e. in your application or script. The Postgres server doesn't need to know what file you're copying to, it just spits out the data and the client puts it somewhere. The underlying syntax for this is the The
Note that there is no terminating From the docs:
Your application programming language may also have support for pushing or fetching the data, but you cannot generally use |
|||||||||||||||||||||
|
There are several solutions: 1
|
|
IMHO the first option is error prone, because it doesn't include proper escaping of comma in exported data.
– Piohen
May 6 '13 at 21:07
|
||
|
@Piohen as far as I remember it does because it will quote strings, but I'm not 100% sure, better to test.
– sorin
May 7 '13 at 9:09
|
||
|
Also, psql doesn't quote cell values, so if ANY of your data uses the delimiter, your file will be corrupted.
– Cerin
Apr 8 '14 at 21:39
|
||
|
@Cerin -t is a synonym for --tuples-only (turn off printing of column names and result row count footers, etc.) - omit it to get column headers
– ic3b3rg
Jun 5 '14 at 21:40
|
||
|
Just tested the comma-escaping claim—it’s true, method #1 does not escape commas in values.
– MrColes
Sep 17 '14 at 21:07
|
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 find your cvs file in this location
or edit using nano:
|
|||||||||||||||||||||
|
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. |
|||||
|
I had to use the \COPY because I received the error message:
So I used:
and it is functioning |
||||
|
See |
|||||
|
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'm working on AWS Redshift, which does not support the My BI tool supports tab-delimited CSVs though, so I used the following:
|
||||
|
I've written a little tool called
The query is assumed to be the contents of STDIN, if present, or the last argument. All other arguments are forwarded to psql except for these:
|
|||
|
JackDB, a database client in your web browser, makes this really easy. Especially if you're on Heroku. It lets you connect to remote databases and run SQL queries on them. Once your DB is connected, you can run a query and export to CSV or TXT (see bottom right). Note: I'm in no way affiliated with JackDB. I currently use their free services and think it's a great product. |
|||||
|
If you have longer query and you like to use psql then put your query to a file and use the following command:
|
|||
|