PostgreSQL unterminated quoted identifier

后端 未结 1 922
感情败类
感情败类 2021-01-28 18:20

I have this groovy code which drops a remote postgres schema from commandline:

def dropSchema = \"psql --dbname=postgres://$user:$pass@localhost:$port/$targetDb          


        
相关标签:
1条回答
  • 2021-01-28 18:46

    Never ever use a string with .execute() like "ls 'my fancy file'".execute(). It splits on whitespace and that is most likely never what you want (same as ["ls", "'my", "fancy", "file'"].execute() for that example).

    Also .execute() runs the command via the regular exec of your OS -- not the shell. So quoting or other things, that needs to be done for a shell command actually make things worse - since no shell is involved to interpret your intention.

    Instead use an array, where all params are their own (don't quote for a shell, that is never used)

    [
     "psql", 
     "--dbname=postgres://$user:$pass@localhost:$port/$targetDb", 
     "-c", "DROP SCHEMA ${applicationName}_${uid} CASCADE;"
    ].execute()
    

    If you prefer to reuse an existing shell command, then run it with a shell:

    ["/bin/sh", "-c", "psql ... -c \"DROP ...\" ..."].execute()
    

    Here you have to quote for the shell, as it is executed like a shell command.

    0 讨论(0)
提交回复
热议问题