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)
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.
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.confand change theline to end with "trust" instead of "md5". Then restart your postgres.