postgresql - Postgres: \copy syntax error in .sql file -


i'm trying write script copies data crosstab query .csv file in postgres 8.4. able run command in psql command line when put command in file , run using -f option, syntax error.

here's example of i'm looking @ (from this great answer):

create temp table t (   section   text  ,status    text  ,ct        integer  );  insert t values  ('a', 'active', 1), ('a', 'inactive', 2) ,('b', 'active', 4), ('b', 'inactive', 5)                    , ('c', 'inactive', 7);  \copy ( select * crosstab(        'select section, status, ct           t         order  1,2'         ,$$values ('active'::text), ('inactive')$$) ct ("section" text, "active" int, "inactive" int) ) 'test.csv' header csv 

i run , following syntax error:

$ psql [system specific] -f copy_test.sql create table insert 0 5 psql:copy_test.sql:12: \copy: parse error @ end of line psql:copy_test.sql:19: error:  syntax error @ or near ")" line 7: ) 'test.csv' header csv         ^ 

a similar exercise doing simple query without crosstab works without incident.

what causing syntax error , how can copy table csv file using script file?

psql thinks first command \copy ( , lines below unrelated statement. meta-commands aren't spread on multiple lines, because newline is terminator them.

relevant excerpts psql manpage emphasis added:

meta-commands

anything enter in psql begins unquoted backslash psql meta-command processed psql itself. these commands make psql more useful administration or scripting. meta-commands called slash or backslash commands.
....
....(skipped)

parsing arguments stops @ end of line, or when unquoted backslash found. unquoted backslash taken beginning of new meta-command. special sequence \\ (two backslashes) marks end of arguments , continues parsing sql commands, if any. way sql , psql commands can freely mixed on line. but in case, arguments of meta-command cannot continue beyond end of line.

so first error \copy ( failing, lines below interpreted independent select looks fine until line 7 when there spurious closing parenthesis.

as told in comments, fix cram whole meta-command single line.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -