skip to Main Content

I have a script with this command

/usr/bin/psql postgres -h localhost -U postgres -c "GRANT CONNECT ON DATABASE "famdb-develop" TO fullwood;"

When I ran it, I got this error:

ERROR:  syntax error at or near "-"
LINE 1: GRANT CONNECT ON DATABASE famdb-develop TO fullwood;

How can I escape the dash in this command?

2

Answers


  1. Looks like the quotes aren’t being passed down to PostgreSQL. You need to escape them by prefixing them with backslashes ():

    /usr/bin/psql postgres -h localhost -U postgres -c "GRANT CONNECT ON DATABASE "famdb-develop" TO fullwood;"
    # Here -----------------------------------------------------------------------^--------------^
    
    Login or Signup to reply.
  2. As you don’t have variables in your sql you could use single quotes

    /usr/bin/psql postgres -h localhost -U postgres -c 'GRANT CONNECT ON DATABASE "famdb-develop" TO fullwood;'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search