Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to automate the creation of a PostgreSQL user with a bash script.

I'm specifying the initial password as prompted by the "createuser" command, and I'd like to do it in bash, which I think implies a here document.

Yet the prompt answers I've specified don't make it to the "createuser" command! Here is directly entering the script on the command line:

-bash-4.1$ createuser mynewuser -P <<ENDBEFORETHIS
> fakepassword
> fakepassword
> n
> n
> n
Enter password for new role: 

Why is that?

  • How can I get the input to the createuser script?
  • is there a more effective way of doing this?

For those just joining us, the "<<" is known as a here-document or a "heredoc", which essentially pipes the next few lines to the command as stdin. It runs until the matching label specified after the "<<".

share|improve this question

migrated from Dec 21 '12 at 5:43

This question came from our site for professional and enthusiast programmers.

I don't understand your example. Do you want the user to actually type a bunch of stuff followed by SETPASSWORD? – noa Dec 21 '12 at 4:34
I'll update the question to explain what a heredoc is – MonkeyWidget Dec 21 '12 at 19:23
for clarity, you should change both "SETPASSWORD" to "ENDBEFORETHIS" ^^ that way non-familiar users of bash won't be expecting bash to incorporate the "SETPASSWORD" string into the input stream. – Olivier Dulac Dec 28 '12 at 14:23
can a : { echo fakepassword ; sleep 1s ; ....... ; } | createuser mynewuser -P work ? (I can't test right now). Or do use expect, pretty good program for these kind of situations. – Olivier Dulac Dec 28 '12 at 14:26
Good thinking Olivier! – MonkeyWidget Sep 22 '13 at 4:39
up vote 1 down vote accepted

The password prompt can be avoided by issuing directly the CREATE USER SQL command, which is what the createuser command would ultimately do, anyway.

~$ psql << EODOC
>create user foo password 'bar';
share|improve this answer

Is there a more effective way of doing this?

You should probably look into using expect(1).

share|improve this answer
probably, especially if there really is some "don't accept through stdin" thing on these password answers – MonkeyWidget Dec 21 '12 at 19:21

For security reasons passwords will not be accepted through stdin. using expect will help u on this.

share|improve this answer
what's weird is this worked a few months ago. I can imagine it perhaps previously working only due to a security bug? – MonkeyWidget Dec 21 '12 at 19:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.