0

If I try to connect with ActiveRecord to a localhost postgresql database with the following connection parameters :

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  username: 'test',
  host: 'localhost',
  password: '123456'
)

I get

FATAL:  password authentication failed for user "test" (PG::Error)

And with :

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  username: 'test',
  host: 'localhost'
)

I get

  fe_sendauth: no password supplied (PG::ConnectionBad)

My pg_hba.conf lines are :

local   all             all                                     trust
local   all             postgres                                peer

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

Why is ActiveRecord is not using the trust method. I set it for all database and all users (first line).

Cédric ZUGER
  • 422
  • 4
  • 12

1 Answers1

1

The trick is the host: 'localhost' line.

If this line is present in the connections parameters, then the driver try to make a localhost network connection rather than a sock connection.

So the host lines of the pg_hba.conf file are used rather than the local lines. And then the connection rightly ask you for a password.

If you remove this line and connect with

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  username: 'test'
)

Then you should be fine and be able to connect to your local database in trust mode.

Cédric ZUGER
  • 422
  • 4
  • 12