Short tip: PostgreSQL error “SQLSTATE[08006] [7] FATAL: Ident authentication failed”

While testing a web application that uses PHP PDO to establish a connection to a PostgreSQL database I stumbled upon the following error message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08006] [7] FATAL: Ident authentication failed for user "user"' in xxx.php

Surprisingly I was able to create a connection using the console with the user credentials:

$ psql -d db -U user

By the way: I’m always creating PostgreSQL databases and users like this:

# useradd user; passwd user
# su - postgres
$ psql
CREATE USER user WITH PASSWORD pass;
CREATE DATABASE db;
GRANT ALL PRIVILEGES ON DATABASE db to user;
q

In my case the PostgreSQL authentification configuration (Host-based authentification) was the fault cause – I had to alter the following lines in the file /var/lib/pgsql/data/pg_hba.conf:

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

Unlike the default configuration MD5 hashed password are now used instead of ident authentification (authentification using ident server). It is important to make this setting for IPv4 as well as IPv6. It might be sufficient to restart the database server after altering the configuration – reloading the configuration did not apply these changes in my case:

# service postgresql restart

Afterwards I was able to authenticate using PHP PDO. 🙂

Sharing is caring

2 comments Write a comment

Leave a Reply