Php – PostgreSQL pg_hba.conf with “password” auth wouldn’t work with PHP pg_connect

authenticationPHPpostgresql

I've recently experimented with the settings in pg_hba.conf. I read the PostgreSQL documentation and I though that the "password" auth method is what I want. There are many people that have access to the server PostgreSQL is working on so I don't want the "trust" method. So I changed it. But then PHP stopped working with the database.

The message I get is "Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "myuser" in /my/path/to/connection/class.php on line 35". It is kind of strange because I can connect via phppgadmin without any problems and also I can connect from my home computer with psql – again without any problems.

This is my pg_hba.conf:

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     password
# IPv4 local connections:
host    all             all             127.0.0.1/32            password
# IPv6 local connections:
host    all             all             ::1/128                 password

The connection string I'm using with pg_conenct is:

$connect_string = "host=localhost port=5432 dbname=mydbname user=auser password=apassword";
$dbConnection = pg_connect($connection_string);

Does anybody know why is this happening ? Did I misconfigured something ?

Best Answer

It appears that when you have password that contains slashes (\ /) you need to escape them in the pg_connect connection url. After escaping that I had no problems connecting to the database via php. Hope this helps somebody having this problem :)