I'm just wondering if it's possible to execute a command inside a bash script as a specified user and supply the password in one string.

At the moment I have


su -c postgres $COMMAND

But this is asking me for a password which I won't be able to enter all the time as this script is going to be running in a cron every hour


Several possibilities:


If you can configure it in a way that is both secure and flexible enough for your calls then you can use sudo to allow the script user to run the commands as user postgres without being asked for a password.

sudo with wrapper script

You can write a wrapper script which is writable by postgres or root only and checks that the call (script parameters) is correct. sudo can allow the script user to call the wrapper script with random parameters as postgres.


If there was only one command line to be executed then you could use SSH. You can bind a certain key to a command i.e. this key cannot be used for anything else.

| improve this answer | |

We can use pipe for applying password and I have used it and it work perfectly well.


su -c postgres $COMMAND | passwd "enter your password here"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.