5

I want to use the \copy command to make csv file with relative path. I used a query to make from test table to csv file named test.csv

\copy (SELECT * FROM test) to './test.csv' with csv

But in postgresql pgadmin4, it shows that \copy command as a syntax error (there is an underline under the word '\copy') and shows a message like below.

ERROR:  syntax error at or near "/"
LINE 2: /copy (SELECT * FROM test) to './persons_client.csv' with cs...
        ^
********** Error **********

ERROR: syntax error at or near "/"
SQL state: 42601
Character: 2

How can I solve this problem?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
윤성필
  • 85
  • 1
  • 1
  • 7

2 Answers2

10

\copy is a meta-command of the default command-line interface psql. You cannot run it from the "Query Tool" of pgAdmin4 (or any other SQL client). Run it from psql instead. (You can open a "PSQL tool" in modern pgAdmin4.)

psql's \copy is a client-side wrapper for the SQL-command COPY. If you have access to the database server (and the necessary privileges) you may be able to use SQL COPY instead.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
5

What I did to solve this problem was to execute:

psql=# copy tmp from '/path/to/file.csv' with delimiter ',' csv header encoding 'windows-1251';
abautista
  • 2,410
  • 5
  • 41
  • 72