Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

When I use the CLI to connect to my database, everything works fine.

psql dbname postgres

psql ask me for the password I set before (with ALTER) and I get connected.

In my pg_hba.conf file I get the following line:

local   all             postgres                                md5

But when I try the same things using node-postgres, I always get an error:

could not connect to postgres { [error: Ident authentication failed for user "postgres"]

The code I am using is basic (Let say my password is mypwd)

var pg = require('pg');
var conString = "postgres://postgres:mypwd@localhost/database";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    //output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
    client.end();
  });
});

Do you have any ideas?

[EDIT: My full pg_hba.conf]

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             postgres                                md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
share|improve this question
    
It's strange but your nodejs code try to connect to database with ident aut-method, not md5. Can you add full pg_hba.conf to your post? – alexander.polomodov Mar 15 at 13:27

There is a difference between "local" and "localhost". The first will connect over a unix domain socket the second over tcp/ip to (typically) 127.0.0.1

You should see separate lines in pg_hba.conf for them.

You can usually connect to a unix-domain socket by putting the path (e.g. /var/run/postgresql/) in place of the server-name.

share|improve this answer

Actually, the problem was in the pg_hba.conf file as alexander.polomodov noticed. I just changed the "ident" words by "md5" everywhere and it works. I don't see why I need to put it in the two lines:

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

So if you can explain, feel free to comment.

share|improve this answer
    
See Richard Huxton answer for explanation. – MarAja Mar 15 at 13:34

Your Answer

 
discard

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.