How to export table data from PostgreSQL (pgAdmin) to CSV file?
Solution 1
From the query editor, once you have executed your query, you just have to click on the "Download as CSV (F8)" button or use F8 key.
Source pgAdmin 4 Query Toolbar
Solution 2
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.
Solution 3
You have to remove the double quotes:
COPY (select * from product_template) TO 'D:\Product_template_Output.csv'
DELIMITER ',' CSV HEADER;
Dhouha
Updated on May 06, 2022Comments
-
Dhouha almost 2 years
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 ?
-
ceving over 3 years
\COPY
is better thanCOPY
, because\COPY
works also for normal users. -
Federico about 2 yearsAlso see stackoverflow.com/a/60035650/299421
-
Procrastinator about 2 yearsWhile this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
-
Raghavendra Acharya almost 2 yearsthis would only select the output without the column name.