3

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?

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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?
depends if you want a separate user for that db. i nearly always do, since separation of privileges is usually a good idea :)
3

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

2 Comments

I get this: /usr/lib/postgresql/8.4/bin/createdb: invalid option -- 'u'
My bad, case sensitive (should be U), have edited answer. Dbl check the link there if you have questions/ideas about the syntax.

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.