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.
I'm working on AWS Redshift, which does not support the
COPY TO
feature.My BI tool supports tab-delimited CSVs though, so I used the following:
I've written a little tool called
psql2csv
that encapsulates theCOPY query TO STDOUT
pattern, resulting in proper CSV. It's interface is similar topsql
.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:
CSV Export Unification
This information isn't really well represented. As this is the second time I've needed to derive this, I'll put this here to remind myself if nothing else.
Really the best way to do this (get CSV out of postgres) is to use the
COPY ... TO STDOUT
command. Though you don't want to do it the way shown in the answers here. The correct way to use the command is:Remember just one command!
It's great for use over ssh:
It's great for use inside docker over ssh:
It's even great on the local machine:
Or inside docker on the local machine?:
Or on a kubernetes cluster, in docker, over HTTPS??:
So versatile, much commas!
Do you even?
Yes I did, here are my notes:
The COPYses
Using
/copy
effectively executes file operations on whatever system thepsql
command is running on, as the user who is executing it1. If you connect to a remote server, it's simple to copy data files on the system executingpsql
to/from the remote server.COPY
executes file operations on the server as the backend process user account (defaultpostgres
), file paths and permissions are checked and applied accordingly. If usingTO STDOUT
then file permissions checks are bypassed.Both of these options require subsequent file movement if
psql
is not executing on the system where you want the resultant CSV to ultimately reside. This is the most likely case, in my experience, when you mostly work with remote servers.It is more complex to configure something like a TCP/IP tunnel over ssh to a remote system for simple CSV output, but for other output formats (binary) it may be better to
/copy
over a tunneled connection, executing a localpsql
. In a similar vein, for large imports, moving the source file to the server and usingCOPY
is probably the highest-performance option.PSQL Parameters
With psql parameters you can format the output like CSV but there are downsides like having to remember to disable the pager and not getting headers:
Other Tools
No, I just want to get CSV out of my server without compiling and/or installing a tool.
I tried several things but few of them were able to give me the desired CSV with header details.
Here is what worked for me.
New version - psql 12 - will support
--csv
.Usage:
In terminal (while connected to the db) set output to the cvs file
1) Set field seperator to
','
:2) Set output format unaligned:
3) Show only tuples:
4) Set output:
5) Execute your query:
6) Output:
You will then be able to find your csv file in this location:
Copy it using the
scp
command or edit using nano: