0

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.

1
  • tr -d '\n' < tocsv.sql | psql Commented Jun 10, 2022 at 22:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.