I am working on a script that creates a Postgres user with a variable input from a source file. As the user 'root' I run the following script:
su - postgres <<'EOF' . /var.src psql CREATE ROLE john; ALTER USER john WITH PASSWORD '$JOHNPASSWORD'; ALTER ROLE john WITH LOGIN; CREATE DATABASE john; ALTER DATABASE john OWNER TO john; \q EOF
I'm creating a user john and a database john, however john's password is in the var.src. Everywhere I look I get commands similar to this:
sudo -u postgres psql -c "ALTER USER john WITH PASSWORD '$JOHNPASSWORD';"
Replacing the password with $JOHNPASSWORD requires an escape I assume. I have tried escaping the single quote with a backslash, double-quotes, etc. Nothing. I haven't been able to find how I can escape the single quote so that it can read the variable from var.src.
Maybe it has something to do with how I'm approaching this entirely?