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:
1Fatal 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:
1$ psql -d db -U user
By the way: I'm always creating PostgreSQL databases and users like this:
1# useradd user; passwd user
2# su - postgres
3$ psql
4CREATE USER user WITH PASSWORD pass;
5CREATE DATABASE db;
6GRANT ALL PRIVILEGES ON DATABASE db to user;
7q
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
:
1# IPv4 local connections:
2host all all 127.0.0.1/32 md5
3# IPv6 local connections:
4host 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:
1# service postgresql restart
Afterwards I was able to authenticate using PHP PDO. 🙂