0

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
1
  • 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? Commented Mar 15, 2016 at 13:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

See Richard Huxton answer for explanation.
Mark Richard's answer as the correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.