Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a user for Postgres from the command line for bash automation

I am using Ubuntu 12.04 and Postgress 9.2.

I need to create this user with this password e.g.

postgres://admin:[email protected]:5432 

How to do that from the command line? I need to automate with a bash script. I have a fresh install.

like image 781
Tampa Avatar asked Sep 10 '13 09:09

Tampa


People also ask

How do I connect to postgres from terminal?

Username [postgres]: The default username for postgres is postgres. (If you are using Advanced Server it is enterprisedb.) On a Mac or Windows, you are able to connect to the default instance by simply hitting enter at the shell or command prompt when trying to run psql and keying in the password.

How do I create a user in pgAdmin?

Click in the Email field, and provide an email address for the user. Use the drop-down list box next to Role to select whether a user is an Administrator or a User. Select Administrator if the user will have administrative privileges within the pgAdmin client. Select User to create a non-administrative user account.


2 Answers

This will create user admin with password test101 on localhost:

psql -c "CREATE USER admin WITH PASSWORD 'test101';" 
like image 116
Tomas Greif Avatar answered Sep 22 '22 02:09

Tomas Greif


To run it from any user just add the sudo -u postgres to it:

sudo -u postgres bash -c "psql -c \"CREATE USER vagrant WITH PASSWORD 'vagrant';\"" 
like image 20
mimoralea Avatar answered Sep 21 '22 02:09

mimoralea