Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a shell script which runs on deployment and I have these lines:

# Database
createdb $DBNAME
createuser -D -A $DBNAME

However, in my logs I get this error:

  • createdb: could not connect to database postgres: FATAL: Ident authentication failed for user "root"
  • createuser: could not connect to database postgres: FATAL: Ident authentication failed for user "root"

Would anyone mind telling me what is going wrong here and how I can correct my lines. Surely root should have permission to do this?

share|improve this question
up vote 3 down vote accepted

the best way to do this, especially if you want your script to be portable, is:

su --login postgres --command "createdb $DBNAME"

this should be safer, more secure, and more portable than using -U. i do it this way in all my posgreSQL scripts. you might find it a useful technique. obviously it still needs to be run as a root user (e.g. with sudo).

share|improve this answer
    
Ok, thank you Simon. – Jimmy Dec 8 '12 at 23:52
    
I install postgreSQL with apt-get which makes a postgres account automatically. Is there any need to create a seperate user account like I did in my first post? – Jimmy Dec 8 '12 at 23:53
    
depends if you want a separate user for that db. i nearly always do, since separation of privileges is usually a good idea :) – simon Dec 8 '12 at 23:53

If your DB is secured, you need to connect as a DB user, not as a user of the OS. For example:

createdb -U dbrootuser -W $DBNAME 

See this link for full syntax

share|improve this answer
    
I get this: /usr/lib/postgresql/8.4/bin/createdb: invalid option -- 'u' – Jimmy Dec 8 '12 at 23:24
    
My bad, case sensitive (should be U), have edited answer. Dbl check the link there if you have questions/ideas about the syntax. – James Hornitzky Dec 8 '12 at 23:35

Your Answer

 
discard

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.