1

I'm trying to connect to postgres via node using the md5 authentication method.

My pg_hba_conf file looks like this:

 "local" is for Unix domain socket connections only 
 local   all           all                                      md5 
 IPv4 local connections:
 host    all             all            127.0.0.1/32            md5
 IPv6 local connections: 
 host    all             all            ::1/128                 md5

I can connect to the database via psql without any problems, but my question is how do you create the connection string within node to connect to postgres when via md5? If I change the pg_hba.conf to use 'password' as the authentication method, then I can connect to the database with the following:

let connectionString = postgres://user:password@localhost:5432/database';

I had thought that I could md5hash my password within the connectionString e.g:

let password = crypto.createHash('md5').update(my_password).digest('hex');

let connectionString = 'postgres://user:' + password + '@localhost:5432/database';

But this didn't work :-(

Can someone point me in the right direction as to how you access postgres via Node using md5 authentication?

Cheers

hloughrey
  • 958
  • 3
  • 11
  • 22
  • 1
    Why do you think it should be a `md5(password)`? – zerkms Oct 25 '16 at 20:50
  • The documentation says The password-based authentication methods are md5 and password. These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively. So I had thought that if i md5 hashed my password and forwarded it in the connection string postgres would be able understand it. – hloughrey Oct 25 '16 at 20:56
  • https://www.postgresql.org/docs/devel/static/protocol-flow.html#AEN92524 Check the `AuthenticationMD5Password` item. It is not as simple as `md5(password)`. This is slightly friendlier explanation: http://stackoverflow.com/a/14941263/251311 – zerkms Oct 25 '16 at 20:58

1 Answers1

0

Use:

let connectionString = 'postgres://user:' + my_password + '@localhost:5432/database';

Documentation says:

var client = new Client('postgres://brian:mypassword@localhost:5432/dev');

It doesn't mention md5 at all. It's database driver job to encode and send properly encoded password.

Radek Postołowicz
  • 4,506
  • 2
  • 30
  • 47
  • But this connection string only works if the pg_hba.conf file has an authentication method of password. I'd like to use md5 so that the password isn't sent as clear-text. – hloughrey Oct 26 '16 at 06:15