16

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?

2 Answers 2

13

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
Sign up to request clarification or add additional context in comments.

Comments

5

From the shell, you could use an here-document, like:

#!/bin/sh

psql -U postgres postgres <<OMG
 CREATE USER lutser password '`cat /tmp/xxx`' ;
OMG

BTW: I'don't think it is a good idea to store the password in /tmp/, not even temporally.

2 Comments

I understand but ... how do you enter a password programatically without having that somewhere? The way I do it: I have them in a local file (plain text) indexed by password id (that is, the passwords file is password_id:password. This file is protected, and I access it with sudo. This way I can centralize my password management, by having a simple service providing access to this file.
@blueFast these days in the cloud the best way to do it is to pass the password in via a secure mechanism like a secure key manager like AWS vault. You can do that via environment parameters on startup. That way they can be encrypted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.