Hi guys, how to switch user in ubuntu and exec cmds as that user in sh step of J . . .

Pradeep Murthy:
Hi guys, how to switch user in ubuntu and exec cmds as that user in sh step of Jenkins pipeline.? I am getting this error because psql is executed as root
script {
sh ‘’’
su - postgres psql -c “CREATE USER myuser WITH PASSWORD ‘mypassword’;”
‘’’
}

su - postgres psql -c CREATE USER myuser WITH PASSWORD 'mypassword';
psql: line 1: CREATE: command not found

Al West:
Your command should look like this:

su - postgres -c "psql -c \\"CREATE USER myuser WITH PASSWORD 'mypassword';\\""

You need to escape the double quotes in the psql command and also use the -c for both the psql command and su.

Pradeep Murthy:
Thanks. It didn’t work says No such file. Changing the peer auth to trust works.