Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 229
Dhouha Avatar asked Dec 14 '18 10:12

Dhouha


People also ask

How do I export data from pgAdmin to CSV?

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. Show activity on this post. 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.

How do I export a table from PostgreSQL to CSV?

The easiest but the most efficient way to export data from a Postgres table to a CSV file is by using the COPY command. COPY command generates a CSV file on the Database Server. You can export the entire table or the results of a query to a CSV file with the COPY TO command.

How do I export a table from pgAdmin?

Step 1: Visit your schema section and select the table you wish to export. Step 2: Right-click on the table name to show up the available options. Step 3: Select the “Import/Export” option. When you click on it, the export data pgAdmin window will appear.


2 Answers

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

like image 138
Sebastien Avatar answered Oct 01 '22 09:10

Sebastien


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.

like image 22
saviour123 Avatar answered Oct 01 '22 11:10

saviour123