Home > Software engineering >  Problem with quoting in shell script psql
Problem with quoting in shell script psql

Time:11-10

I'm trying to use the following in a shell (/bin/sh) script but I keep getting errors:

su postgres -c "/usr/bin/psql -c \'CREATE ROLE user WITH SUPERUSER LOGIN PASSWORD \'$password;"

It seems that something with the quoting is wrong, but I can't find what is it. I should pass the variable $password and the command as postgres user. Can someone help me?

Thanks in advance

CodePudding user response:

Your quotes are not even matched. You can escape double quotes within double quotes, and there is no need to escape single quotes.

su postgres -c "/usr/bin/psql -c \"CREATE ROLE username WITH SUPERUSER LOGIN PASSWORD '$password';\""

Note that user is not a valid name in SQL.

CodePudding user response:

How about this?

passwort=foobar; su postgres -c "/usr/bin/psql -c 'CREATE ROLE user WITH SUPERUSER LOGIN PASSWORD \"$password\"';"
  • Related