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

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 ?



Solution 1:[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

Export button location

Solution 2:[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:[3]

You have to remove the double quotes:

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

Solution 4:[4]

Try this command:

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

Solution 5:[5]

In PgAdmin export option is available in file menu.Execute the query, and then we can view the data in the Output pane. Click on the menu FILE -> EXPORT from query window.

PSQL to export data

COPY noviceusers(code, name) FROM 'C:\noviceusers.csv' DELIMITER ',' CSV HEADER;

https://www.novicetechie.com/2019/12/export-postgresql-data-in-to-excel-file.html for reference.

Solution 6:[6]

If your PgAdmin instance resides in a remote server, the aforementioned solutions might not be handy for you if you do not have remote access to the server. In this case, simply select all the query data and copy it. Open an excel file and you could paste it. Simple !! Tweaked. You might have tough time if your query result is too much though. enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sebastien
Solution 2
Solution 3 Laurenz Albe
Solution 4 Bhavana
Solution 5
Solution 6 Roshith S