Export a PostgreSQL query to a CSV
When using psql, the general method for querying data is to log into a database in PuTTY:
psql Database_Name Username
Then enter a command in the following format:
SELECT * From Table_Name WHERE Column_Name LIKE 'Foo%';
In this example, we want to look into a Table called “Table_Name” and search everything (*) in the column “Column_Name” for entries that contain “Foo” at the start (hence the % wildcard). There are many variations of this
SELECT function. This is beyond the scope of this article.
If we wanted to send all of the results from our database query to a CSV file, we can do this via the
\COPY (SELECT * From Table_Name WHERE Column_Name LIKE 'Foo%') TO 'csv-output/csv_file.csv' WITH CSV DELIMITER ',' HEADER
What this says is that we want to copy the output from our query to a CSV file called “csv_file.csv” on our server in a folder called “csv-output”. We have specified a comma as the delimiter and want the table headers included. You will notice that we need to exclude the trailing semicolon in our database query for this command.
\COPY command (as opposed to
COPY) performs a frontend (client) copy operaton. This is an operation that runs an SQL
COPY command, but instead of the server reading or writing the specified file, psql reads or writes the file and routes the data between the server and the local file system. This means that file accessibility and privileges are those of the local user, not the server and no SQL superuser privileges are required. (psql actually initiates the
STDCOPY function when using
The question then arises; where specifically is the CSV file?
To answer that question, we need to see where
STDCOPY is placing it, which is the current working directory. To determine the current working directory in psql, enter the following command:
This is the location where the CSV file will be found (or the /csv-output/ subdirectory in our case). The file can then be accessed and downloaded via WinSCP or another FTP programme.
Please note that if you are running PostgreSQL on a Windows server, file paths will need to utilise a double-backslash.