Node-postgres: Passwordless login only works with the native client

Created on 15 Oct 2012  路  5Comments  路  Source: brianc/node-postgres

Below shows me trying to connect to the "tngfooty" database using a unix domain socket without specifying a password. If I use the native client, it works. With the javascript client, authenticaton fails.

pg = require 'pg'
# If this line is omitted, password authentication will fail.
# pg = pg.native
pg.connect 'postgresql:///tngfooty', (err, client) ->
    console.log err
    client.query 'select now() as when', (err, result) ->
        console.log 'Row count: %d', result.rows.length
        console.log 'When: %s', result.rows[0].when

(sorry for the coffeescript)

bug

Most helpful comment

I was getting this because my dev machine defaulted to trust for local TCP connections, but my test/deploy machine defaulted to md5. To fix: sudo vim /etc/postgresql/9.1/main/pg_hba.conf and change the

host    all             all             127.0.0.1/32           md5

line to end with "trust" instead of "md5". Then restart your postgres.

All 5 comments

That is strange to fail only with the javascript bindings. Do you have a copy of the error?

It's not strange because the javascript bindings sends a username and password even if you dont want them to. In the above example it sends process.env.USER as the username and null as the password even though it shouldn't do username authentication at all.

{ [error: password authentication failed for user "bjourne"]
  length: 96,
  name: 'error',
  severity: 'FATAL',
  code: '28000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'auth.c',
  line: '273',
  routine: 'auth_failed' }

confirmed here

you can set pg.defaults.user and pg.defaults.password to null or an empty string to send nothing. Or use a config object instead of a connection string.

I was getting this because my dev machine defaulted to trust for local TCP connections, but my test/deploy machine defaulted to md5. To fix: sudo vim /etc/postgresql/9.1/main/pg_hba.conf and change the

host    all             all             127.0.0.1/32           md5

line to end with "trust" instead of "md5". Then restart your postgres.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmedBHameed picture AhmedBHameed  路  3Comments

v1co1n picture v1co1n  路  4Comments

wrod7 picture wrod7  路  4Comments

KeynesYouDigIt picture KeynesYouDigIt  路  3Comments

gregallenvt picture gregallenvt  路  3Comments