Reputation: 769
I'm creating an export to csv that's a rather long query, as such, I placed the \copy command in a new file. The problem is that the newlines seem to break my command, is there a way I'm able to execute this command with line breaks?
For example:
\copy (select
field1
, field2
FROM table
) to '/tmp/out.csv' csv;
I'm currently running that command from a sql file as such:
psql -f tocsv.sql
I don't think it matters that I'm running it from a file. Is there a way to execute the copy command with the line breaks in my query? I'd like to keep them there for legibility.
Upvotes: 0
Views: 419
Reputation: 19580
From here psql in \copy
section:
Tip
Another way to obtain the same result as \copy ... to is to use the SQL COPY ... TO STDOUT command and terminate it with \g filename or \g |program. Unlike \copy, this method allows the command to span multiple lines; also, variable interpolation and backquote expansion can be used.
Upvotes: 1