How Do You Troubleshoot Connecting to a Postgres Database When You Get the Error “FATAL: Ident Authentication Failed”?

Problem scenario
You are trying to establish a connection with a username and password to a Postgres database using PHP.  You have a PHP file like this:  

<?php
   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=contint";
   $credentials = "user=contint password=excellent";

   $db = pg_connect( "$host $port $dbname $credentials"  );
   if(!$db){
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }
?>

Every time you run this above file, we'll call foo.php, you get an error.  You run this:

php foo.php

But this results in an error like this:

'PHP Warning:  pg_connect(): Unable to connect to PostgreSQL server: FATAL:  Ident authentication failed for user "contint" in /tmp/contint.php on line 7
Error : Unable to open database'

Your pg_hba.conf file has a stanza like this:

host     all    all    127.0.0.1/32    md5

You therefore think that because you are passing a username and password, the authentication should be successful.  The database's owner is the role (or username) that you are using.  You restarted Postgresql services.  Why do you keep getting this error?

Solution
Look in the /var/lib/pgsql/data/pg_hba.conf file.  You may find this stanza above the stanza with the "md5":

host    all    all    127.0.0.1/32    ident

Comment out the above line with "ident" using a # at the far left.  Try running the PHP script again.

Leave a comment

Your email address will not be published. Required fields are marked *