PG::ConnectionBad: fe_sendauth: no password supplied

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

When I attempt to run "rake test" on a local postgres database, it throws the above exception.

Here is my pg_hba.conf file: # Database administrative login by Unix domain socket local all postgres peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD  # "local" is for Unix domain socket connections only local   all             username                                  peer local   myapp_dev   myapp                               md5 local   myapp_test  myapp                               md5 local   myapp_prod  myapp                               md5 #local   all             all                                     peer # IPv4 local connections: host    all             all             127.0.0.1/32            md5 # IPv6 local connections: host    all             all             ::1/128                 md5 # Allow replication connections from localhost, by a user with the # replication privilege. #local   replication     postgres                                peer #host    replication     postgres        127.0.0.1/32            md5 #host    replication     postgres        ::1/128                 md5 

and here is the relevant section from my database.yml

test:  adapter: postgresql database: myapp_test pool: 5 timeout: 5000 host: localhost username: username password: 

In the real database.yml, 'username' is replaced with my actual user name that I am logged in as. Since the authentication method is defined as 'peer', no password should be required.

I have also taken care to restart Postgres

sudo -u postgres pg_ctlcluster 9.3 main restart 

What else am I missing here?

回答1:

localhost as a host refers to a TCP connection, which means the auth method is md5 (password required) per your pg_hba.conf:

 # IPv4 local connections: host    all             all             127.0.0.1/32            md5 # IPv6 local connections: host    all             all             ::1/128                 md5 

For the peer method to be taken, you'd need to connect through Unix domain sockets, and since you seem to be using a debian-like OS, that means putting /var/run/postgresql in the host field, or nothing at all (it's the default unless environment variables say otherwise).

EDIT: if using database URIs (supported since Rails-4.1, as announced in http://weblog.rubyonrails.org/2014/4/8/Rails-4-1/), the syntax could be:

  • for localhost:
    test: "postgresql://localhost/myapp_test"

  • for the default Unix socket domain (host field left empty):
    test: "postgresql:///myapp_test"



回答2:

Change the code as below and it will work

pg_hba.conf: # IPv4 local connections: host    all             all             127.0.0.1/32            trust # IPv6 local connections: host    all             all             ::1/128                 trust 


回答3:

If your hb_conf has already been modified to force passwords, then make sure your rails app's database configuration includes a password in both development and test environments.

default: &default   adapter: postgresql   encoding: unicode   pool: 5   host: localhost   username: your_user   password: your_password  development:    

I was getting this error when I failed to supply a password for the test database.



回答4:

I met this question, too. I checked my database config, /var/www/myproject/shared/config/database.yml, production: adapter: postgresql pool: 5 timeout: 5000 encoding: utf8 host: localhost database: myproject username: myname password:

I found last paragraph is wrong, the right code is

password:



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!