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

48,279

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

Export button location

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;
Share:
48,279
Dhouha
Author by

Dhouha

Updated on May 06, 2022

Comments

  • Dhouha
    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
    ceving over 3 years
    \COPY is better than COPY, because \COPYworks also for normal users.
  • Federico
    Federico about 2 years
  • Procrastinator
    Procrastinator about 2 years
    While 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
    Raghavendra Acharya almost 2 years
    this would only select the output without the column name.