Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I need to export the resulting data from a query in PostgreSQL to Excel/CSV.
I use PostgreSQL 8.2.11.

SQL error:

ERROR:  relative path not allowed for COPY to file
In statement:

COPY (select distinct(m_price) from m_product)TO '"c:\auto_new.txt"';
share|improve this question
2  
The name is PostgreSQL or Postgres for short. There is no Postgre. – Erwin Brandstetter Nov 14 '11 at 9:28
    
You only need to double-quote the filename if it includes whitespace or special characters. Not necessary for 'C:\\auto_new.txt'. (Not wrong, though.). There is – Erwin Brandstetter Nov 14 '11 at 12:27
    
i tried giving >TO 'C:\\auto_new.txt' but the same error – Ghostman Nov 14 '11 at 12:28
    
There is also a space missing before TO. Not sure whether PostgreSQL 8.2 cares. It would still work in 9.0. – Erwin Brandstetter Nov 14 '11 at 12:52
up vote 24 down vote accepted

Example with Unix-style file name:

COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv' format csv;

Read the manual about COPY (link to version 8.2).
You have to use an absolute path for the target file. Be sure to double quote file names with spaces. Example for MS Windows:

COPY (SELECT * FROM tbl)
TO E'"C:\\Documents and Settings\\Tech\Desktop\\myfile1.csv"' format csv;

In PostgreSQL 8.2, with standard_conforming_strings = off per default, you need to double backslashes, because \ is a special character and interpreted by PostgreSQL. Works in any version. It's all in the fine manual:

filename

 The absolute path name of the input or output file. Windows users might need to use an E'' string and double backslashes used as path separators.

Or the modern syntax with standard_conforming_strings = on (default since Postgres 9.1):

COPY tbl  -- short for (SELECT * FROM tbl)
TO '"C:\Documents and Settings\Tech\Desktop\myfile1.csv"' (format csv);

Or you can also use forward slashes for filenames under Windows.

An alternative is to use the meta-command \copy of the default terminal client psql.

You can also use a GUI like pgadmin and copy / paste from the result grid to Excel for small queries.

Closely related answer:

Similar solution for MySQL:

share|improve this answer
1  
COPY (SELECT * FROM tbl) TO 'C:/Documents and Settings/Tech/Desktop/myfile1.csv'; SQL error: ERROR: relative path not allowed for COPY to file – Ghostman Nov 14 '11 at 9:15
1  
i get the error still??? – Ghostman Nov 14 '11 at 10:04
1  
i still get the error – Ghostman Nov 14 '11 at 10:30
3  
@sanre6: Always remember that COPY handles files local to the server. If your client is on a different machine, use the meta-command \copy of the psql client or some other tool like pgAdmin. – Erwin Brandstetter Jan 4 '12 at 11:05
1  
@AKIWEB: Follow my link above and read the manual. If something is still unclear, ask a question. Comments are not the place. – Erwin Brandstetter Aug 5 '14 at 3:30

This worked for me:

COPY (SELECT * FROM table) 
    TO E'C:\\Program Files (x86)\\PostgreSQL\\8.4\\data\\try.csv';

In my case the problem was with the writing permission to a special folder (though I work as administrator), after changing the path to the original data folder under PostgreSQL I had success.

share|improve this answer
    
What version of PostgreSQL? – Tim Child Mar 21 '13 at 0:25
    
Wonderful. It worked for me in linux. – ALH Jun 16 '14 at 6:17

If you have error like "ERROR: could not open server file "/file": Permission denied" you can fix it that:

Ran through the same problem, and this is the solution I found: Create a new folder (for instance, tmp) under /home $ cd /home make postgres the owner of that folder $ chown -R postgres:postgres tmp copy in tmp the files you want to write into the database, and make sure they also are owned by postgres. That's it. You should be in business after that.

share|improve this answer
    
Or you can copy to /tmp. Worked for me. – dimuthu May 5 '15 at 6:02

In PostgreSQL 9.4 to create to file CSV with the header in Ubuntu:

COPY (SELECT * FROM tbl) TO '/home/user/Desktop/result_sql.csv' WITH CSV HEADER;

Note: The folder must be writable.

share|improve this answer

Several GUI tools like Squirrel, SQL Workbench/J, AnySQL, ExecuteQuery can export to Excel files.

Most of those tools are listed in the PostgreSQL wiki:

http://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQL_GUI_Tools

share|improve this answer

The correct script for postgres (Ubuntu) is:

COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv';
share|improve this answer
    
how can i give that command in psql command line ubuntu? – AhmetMelihBaşbuğ Oct 7 at 12:07
    
psql -c "COPY (SELECT * FROM tbl) TO '/var/lib/postgres/myfile1.csv';" don't forget to add db, user and password if require. -c flag give you option to run command from ubuntu(linux) terminal. – Michael Oct 25 at 16:09
    
I want to put quote around my string values. how can i? @Michael – AhmetMelihBaşbuğ Oct 26 at 6:40
    
You can try ' ' or "\"" – Michael Oct 26 at 7:58

protected by Community Oct 7 at 12:07

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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