How to export table data from PostgreSQL (pgAdmin) to CSV file?

Dhouha picture Dhouha · Dec 14, 2018 · Viewed 20.3k times · Source

I am using pgAdmin version 4.3 and i want to export one table data to CSV file. I used this query

COPY (select * from product_template) TO 'D:\Product_template_Output.csv' DELIMITER ',' CSV HEADER;

but it shows error

a relative path is not allowed to use COPY to a file

How can I resolve this problem any help please ?

Answer

saviour123 picture saviour123 · Dec 14, 2018

Use absolute paths or cd to a known location so that you can ignore the path. For example cd into documents directory then run the commands there.

If you are able to cd into your documents directory, then the command would be like this:

Assuming you are want to use PSQL from the command line. cd ~/Documents && psql -h host -d dbname -U user

\COPY (select * from product_template) TO 'Product_template_Output.csv' DELIMITER ',' CSV HEADER;

The result would be Product_template_Output.csv in your current working directory(Documents folder).

Again using psql.