Ruby-on-rails PG::ConnectionBad: fe_sendauth: 没有提供密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23375740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:35:12  来源:igfitidea点击:

PG::ConnectionBad: fe_sendauth: no password supplied

ruby-on-railspostgresqlauthentication

提问by Lawrence I. Siden

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

当我尝试在本地 postgres 数据库上运行“rake test”时,它会抛出上述异常。

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

这是我的 pg_hba.conf 文件: # 由 Unix 域套接字本地所有 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

这是我的 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.

在真实的 database.yml 中,'username' 被替换为我登录时的实际用户名。由于身份验证方法定义为“对等”,因此不需要密码。

I have also taken care to restart Postgres

我也小心地重新启动了 Postgres

sudo -u postgres pg_ctlcluster 9.3 main restart

What else am I missing here?

我在这里还缺少什么?

采纳答案by Daniel Vérité

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

localhost作为主机是指 TCP 连接,这意味着身份验证方法是md5(需要密码)每个您的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 peermethod 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/postgresqlin the hostfield, or nothing at all (it's the default unless environment variables say otherwise).

对于要采用的peer方法,您需要通过 Unix 域套接字进行连接,并且由于您似乎使用的是类似 debian 的操作系统,这意味着要投入/var/run/postgresql使用host,或者什么都不做(这是默认设置,除非环境变量说除此以外)。

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:

编辑:如果使用数据库 URI(从 Rails-4.1 开始支持,如 http://weblog.rubyonrails.org/2014/4/8/Rails-4-1/ 中所述),语法可能是:

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

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

  • 对于本地主机:
    test: "postgresql://localhost/myapp_test"

  • 对于默认的 Unix 套接字域(主机字段留空):
    test: "postgresql:///myapp_test"

回答by Ravistm

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

Below its explanation:

下面是它的解释:

trust

Allow the connection unconditionally. This method allows anyone that can connect to the PostgreSQL database server to login as any PostgreSQL user they wish, without the need for a password or any other authentication.

md5

Require the client to supply a double-MD5-hashed password for authentication.

相信

无条件允许连接。这种方法允许任何可以连接到 PostgreSQL 数据库服务器的人以他们希望的任何 PostgreSQL 用户身份登录,而无需密码或任何其他身份验证。

MD5

要求客户端提供双重 MD5 哈希密码进行身份验证。

refer for more here

在这里参考更多

回答by s2t2

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.

如果您的 hb_conf 已经被修改为强制使用密码,那么请确保您的 rails 应用程序的数据库配置在开发和测试环境中都包含密码。

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

development:
  <<: *default
  database: your_db_development

test:
  <<: *default
  database: your_db_test

production:
  url: <%= ENV['DATABASE_URL'] %>

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

当我未能为测试数据库提供密码时,我收到此错误。

回答by hofffman

I met this question, too. I checked my database config, /var/www/myproject/shared/config/database.yml:

我也遇到了这个问题。我检查了我的数据库配置,/var/www/myproject/shared/config/database.yml

production: 
adapter: postgresql 
pool: 5 
timeout: 5000 
encoding: utf8 
host: localhost 
database: myproject
username: myname 
password: <%= ENV['name_DATABASE_PASSWORD'] %>

I found the last paragraph is wrong, the right code is

我发现最后一段是错误的,正确的代码是

password: <%= ENV['myproject_DATABASE_PASSWORD'] %>

回答by Jose Paez

I use Postgres App. Which is really great because you don't have to supply username and password in the default part of the database.yml

我使用 Postgres 应用程序。这真的很棒,因为您不必在 database.yml 的默认部分提供用户名和密码

database.yml

数据库.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: my-app_development

test:
  <<: *default
  database: my-app_test

production:
  <<: *default
  database: my-app_production
  username: my-app
  password: <%= ENV['MY-APP_DATABASE_PASSWORD'] %>

My problem: I changed the path and wasn't able to get postgres to work.

我的问题:我改变了路径并且无法让 postgres 工作。

Solution: use the documentationto:

解决方案:使用文档来:

1) Uninstall the app

1) 卸载应用

2) Stop the postgres process using this answer, which says to use sudo pkill -u postgres

2)使用这个答案停止postgres进程,它说使用sudo pkill -u postgres

3) Reinstall and start the app

3)重新安装并启动应用程序

Everything should work fine now, have a great day!

现在一切正常,祝您有美好的一天!

Note: Doing this also solves the problem Postgres.app port 5432 in use

注意:这样做也解决了Postgres.app port 5432 in use的问题

回答by jas-chu

I had this problem, solved by a coworker: You need to set a local user and password in postgres

我遇到了这个问题,由同事解决:您需要在 postgres 中设置本地用户和密码

createuser --interactive --pwprompt

It will ask you for the name of the role (the user) and the password you want to have.

它会询问您角色的名称(用户)和您想要的密码。

Then you have to add this username and password to your database.yml file

然后你必须将此用户名和密码添加到你的 database.yml 文件中