windowspostgresqlcmd

“was unexpected at this time.”


I'm trying to run the following code but is encountering the "was unexpected at this time" error.

(echo COPY (SELECT ta.colA as name, ta.colB as user_e, ta.colC as user_n, ta.activation_dt, ta.creation_dt, MAX(tb.update_dt) as updated_at, MAX(tb.login_dt) as lastest_login, tc.colD as roleFROM tblA ta, tblB tb, tblC tc WHERE ta.id = tb.tb_id AND ta.tc_id = tc.id AND tc.colD <> 'Guest' GROUP BY ta.colA, ta.colB, ta.colC, ta.activation_dt, ta.creation_dt, tc.colD ORDER BY ta.colA, tc.colD^^^) TO 'E:\Details.csv' CSV DELIMITER ',' HEADER;) | psql -h localhost -p 8060 -U uname -d dbase

Looking for some insights please. Thank you.

Screenshot of error encountered


Solution

  • Try adding some quotes around the SQL, and lose the brackets:

    echo "COPY ..." | psql -h localhost -p 8060 -U uname -d dbase
    

    or use -c option:

    psql -h localhost -p 8060 -U uname -d dbase -c "COPY ..."

    I prefer the -c because it works on all OS