0

I am using

\COPY 
(
    SELECT *
    FROM person
    LEFT JOIN car ON car.id=person.car_id
) 
TO 'c:/Users/nick-/Downloads/results.csv'
DELIMITER ',' CSV HEADER;

and am getting the error

ERROR:  syntax error at or near "TO"
LINE 6: TO 'c:/Users/nick-/Downloads/results.csv'

I've also tried COPY rather than \COPY but I get this error

ERROR:  could not open file "c:/Users/nick-/Downloads/results.csv" for writing: Permission denied
HINT:  COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy.

I have tried using all of the below

c:/Users/nick-/Downloads/results.csv

c://Users//nick-//Downloads//results.csv

'c:\Users\nick-\Downloads\results.csv'

'c:\\Users\\nick-\\Downloads\\results.csv'

and none have worked.

user13456931
  • 25
  • 1
  • 5
  • It looks like your Postgres server is running under a different user account which does not have permission to write to your personal home directory (`C:\users\nick`). Either change your `Downloads` directory permissions to allow Postgres access - or have Postgres write to a folder that it does have permission to write to (such as a folder within `C:\Program Files\Postgres\...` or `C:\Users\Public\Documents`) – Dai Sep 04 '20 at 00:42
  • @Dai I just did this, in the `C:\Users\Public\Documents` using both that syntax and `'C:/Users/Public/Documents'` and got `ERROR: syntax error at or near "TO" LINE 6: TO 'C:/Users/Public/Documents'` again. So I have gotten passed the access, but now am having the same problem as listed above. – user13456931 Sep 04 '20 at 00:53
  • What version of Postgres are you using? – Dai Sep 04 '20 at 00:53
  • @Dai I'm using windows verson 13 – user13456931 Sep 04 '20 at 00:58
  • The latest current version of PostgreSQL is 12.4. The version 13 is still in beta. You should be using version 12.4. – Dai Sep 04 '20 at 01:01
  • 1
    Meta-commands (like \COPY) in psql must fit in a single line. See also [Use psql's \copy for a multi-line query](https://stackoverflow.com/q/42404579/1995738) – klin Sep 04 '20 at 01:06

0 Answers0