Search code examples
zshpsql

Why am I getting a syntax error when trying to use -v flag for variables in psql command?


I'm trying to use the following psql command in a zsh shell to send a query to a database:

psql -Atx $mydburl -v myvar=1 -c "SELECT :myvar" 

And I'm getting the following error:

ERROR:  syntax error at or near ":"
LINE 1: SELECT :myvar
               ^

However, when I use a pipe, this works just fine:

echo "SELECT :myvar" | psql -Atx $mydburl -v myvar=1  

And it produces the expected result of:

?column?|1

What is going on here?


Solution

  • That's as documented:

    -c command
    [...]
    command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command.