Artion Karreçi
Artion Karreçi

Reputation: 3

Problem with quoting in shell script psql

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

Upvotes: 0

Views: 124

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246268

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.

Upvotes: 1

Related Questions