Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Create user and database from shell failing

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?

like image 911
Jimmy Avatar asked Dec 06 '25 04:12

Jimmy


2 Answers

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

like image 56
simon Avatar answered Dec 07 '25 20:12

simon


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

like image 42
James Hornitzky Avatar answered Dec 07 '25 18:12

James Hornitzky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!