Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My PHP code which I try to use in Firefox

 // Connecting, selecting database
 $dbconn = pg_connect("host=localhost dbname=masi user=postgres password=abc")
     or die('Could not connect');

I get

Warning: pg_connect() []: Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "postgres" in /var/www/ex1.php on line 3

The problem seems to be in my /etc/postgresql/8.3/main/pg_hba.conf. It seems that I need to add some ip-address to the file.

Codes in my pg_dba.conf

# Database administrative login by UNIX sockets
 local   all         postgres                          ident sameuser


 # "local" is for Unix domain socket connections only
 local   all         all                               ident sameuser
 # IPv4 local connections:
 host    all         all          md5
 # IPv6 local connections:
 host    all         all         ::1/128               md5

The bug seems to be in PostgresSQL, since PHP and Apache2 work. I can access Psql by sudo -u postgres psql, since I have not managed to change the default settings. This is likely the cause of the problem. However, my PHP code uses the default settings so this should not be a problem.

I changed a line in my /etc/apache2/envvars unsuccessfuly:

export APACHE_RUN_USER=postgres     // I changed this line from masi to postgres
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=/var/run/

I get the same error messages.

How can you get the PHP work with PostgreSQL by pg_hba.conf in Ubuntu?

share|improve this question

migrated from Aug 6 '09 at 19:17

This question came from our site for professional and enthusiast programmers.

Did you set a password to the user postgres ? –  Anonymous Aug 5 '09 at 13:27
@Uka: I did not set any password initially. I removed the password from my code. I get a similar error message but with the message that PostgreSQL server: fe_sendauth: no password supplied... –  Masi Aug 5 '09 at 13:33
Strange, I would think the third block (host all all md5) would work for what you want. –  R. Bemrose Aug 5 '09 at 13:40
Please, move this question to to solve the problem. –  Masi Aug 5 '09 at 16:55
This thread is solved. –  Masi Aug 6 '09 at 20:29

3 Answers 3

try with

host    all         all          trust


local   all         all                               trust

link text

share|improve this answer
I run both your codes with and without a password in my PHP code unsuccessfully. I get the same error messages as before. –  Masi Aug 5 '09 at 13:37
@andres-descalzo: It seems PHP does not support trust connections. I've been banging my head against the wall as well for the past 20 minutes... :( –  exhuma Jun 11 '13 at 9:29
up vote 1 down vote accepted

#1 problem

Log in to the user Postgres by

sudo su postgres

Create a new user, for instance, Masi for PostgreSQL by


Then, log in back to you default user.

#2 problem

The default user in Pg has no password. This caused the problem in PHP.

I changed the dbconn to the following

 // independent variables
 $dbHost = "localhost";
 $dbPort = 5432;
 $dbName = "masi";
 $dbUser = "masi";
 $dbPassword = "your-password";

 $conn = "host=$dbHost port=$dbPort dbname=$dbName user=$dbUser password=$dbPassword";

The problem was the password of the default Postgres' account which I do not know. This forced me to create a new account with a password.

I did not get pgAdmin 3 to work without a password in a database.

share|improve this answer
I strongly advise against using WITH SUPERUSER for application roles! If you have root access to the machine you can easily change the auth method of the postgres user in your pg_hba.conf file temporarily. Then, create a user for yourself using md5 as auth method. –  exhuma Jun 11 '13 at 9:35

I solved the problem by adding the following line to pg_dba.conf.

# host    all         all          trust #
share|improve this answer
FYI: This is being downvoted for two reasons: (a) it's commented out as written, so it has no effect, and (b) If you uncomment it it's dangerous: You are allowing ANYONE on the local system to connect to any Postgres database as any user (including the DB superuser) - you're one badly written PHP script away from someone using SQL injection to drop your production databases. –  voretaq7 Sep 16 at 19:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.