How to alter role from command line in postgresql?

postgresql, sh, ubuntu

Solution

What's wrong is the lack of `-c` or `--command` for `su` to indicate that the rest of the line is a command.

But `su` is not needed anyway, because there's already `sudo`. Do this instead:

sudo -u postgres psql -c "ALTER ROLE postgres WITH password 'pass'"

Problem

I'm trying to build a setup script to automate the development environments creation, but I'm having trouble both trying to pipe or using the -c modifier for psql. I've tried: ``` sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass'" ``` and ``` sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass';" ``` Both of which say "ALTER: command not found" I've also tried pipe, but I'm not able to combine it with su correctly eg: I tried something like ``` sudo su postgres echo "ALTER ROLE postgres WITH password 'pass'" | psql ``` But postgres can't execute "echo" And: ``` echo "ALTER ROLE postgres WITH password 'pass'" | sudo su psql ``` Which just doesn't work. So, my first question is: how can I execute this simple command from a sh file? And the second one, less related: how can I use different users in the commands chained with pipe?

Original source