41

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 ?

6 Answers 6

73

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

1
10

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.

1
  • 1
    \COPY is better than COPY, because \COPYworks also for normal users.
    – ceving
    Commented Dec 2, 2020 at 17:01
3

You have to remove the double quotes:

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

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

1
  • this would only select the output without the column name. Commented Jun 2, 2022 at 9:02
0

Try this command:

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

enter image description here

  1. Write your query to select data on the query tool and execute
  2. Click on the download button on the pgAdmin top bar (selected in red)
  3. Rename the file to your liking
  4. Select which folder to save the file

Congrats!!!

Not the answer you're looking for? Browse other questions tagged or ask your own question.