MarAja
MarAja

Reputation: 1597

Postgresql: I can connect with the command line but not with node-postgres

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

Upvotes: 0

Views: 1034

Answers (2)

MarAja
MarAja

Reputation: 1597

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.

Upvotes: 1

Richard Huxton
Richard Huxton

Reputation: 22972

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.

Upvotes: 1

Related Questions