Dhouha
Dhouha

Reputation: 741

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 ?

Upvotes: 43

Views: 105699

Answers (6)

Kamania
Kamania

Reputation: 107

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!!!

Upvotes: 0

Roshith S
Roshith S

Reputation: 351

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

Upvotes: 4

Sebastien
Sebastien

Reputation: 881

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

Upvotes: 76

saviour123
saviour123

Reputation: 1193

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.

Upvotes: 11

Bhavana
Bhavana

Reputation: 1054

Try this command:

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

Upvotes: 0

Laurenz Albe
Laurenz Albe

Reputation: 246043

You have to remove the double quotes:

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

Upvotes: 3

Related Questions