Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter role from command line in postgresql?

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?

like image 807
Alvaro Avatar asked Feb 18 '15 13:02

Alvaro


People also ask

How do I give superuser permissions in PostgreSQL?

Log into PostgreSQL and run the following ALTER USER command to change user test_user to superuser. Replace test_user with username as per your requirement. postgres-# ALTER USER test_user WITH SUPERUSER; In the above command, we use WITH SUPERUSER clause to change user to superuser.


1 Answers

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'"
like image 92
Daniel Vérité Avatar answered Oct 05 '22 04:10

Daniel Vérité