If your having trouble making a connection to a PostgreSQL database, read on:
The reason your database connection may have failed was because you had specified host=localhost in the connection string. PostgreSQL databases are not configured to accept connections via tcp/ip, so specifying a host causes the connection to fail. If the host and port number are left out of the connection string, the the connection is made locally via UNIX sockets, which does allow the connection.
If your database connection string reads: host=127.0.0.1 username=xxx password=yyy dbname=zzz This should just be: username=xxx password=yyy dbname=zzz
Not accepting tcp/ip connection is the default behaviour for a postgres install.
This should now allow your php script to connect to the PostgreSQL database.