Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new postgres user programmatically, without interactive intervention

Tags:

postgresql

I am trying to do this in a script:

» sudo -u postgres createuser -PE -s user1
Enter password for new role:

I do not want postgres to ask for the password interactively, so I want to do:

» sudo -u postgres createuser -PE -s user1 < /tmp/xxx

Where /tmp/xxx contains the password for the new user. But it does not work. How to get this working?

like image 787
blueFast Avatar asked Jun 01 '13 10:06

blueFast


1 Answers

You can do that using plain SQL after connecting as a superuser (e.g. postgres) to the database in question:

create user user1 password 'foobar'

If you need to do this from within a script you can put that into a sql file and then pass this to psql:

» sudo -u postgres psql --file=create_user.sql
like image 170
a_horse_with_no_name Avatar answered Nov 04 '22 16:11

a_horse_with_no_name