如何使用 PostgreSQL 解决 Rails 中的错误 'fe_sendauth: no password supplied'?

How to resolve the error 'fe_sendauth: no password supplied' in Rails using PostgreSQL?

我尝试使用 postgresql 数据库创建我的 Rails 应用程序,但是当我开始 rails server 时,我收到了这个错误:

fe_sendauth: no password supplied

以下是我的操作步骤:

$ sudo apt-get install postgresql postgresql-contrib
$ gem install pg
$ rails new timetracker --database=postgresql --skip-unit-test

我的 database.yml 文件如下所示:

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

development:
  <<: *default
  database: timetracker_development

test:
  <<: *default
  database: timetracker_test

production:
  <<: *default
  database: timetracker_production

这是我的 pg_hba.conf 文件:

# 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             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

# modified by me.
host    all             all             127.0.0.1/32            trust

host    all             all             127.0.0.1/32            trust

host    all             all             ::1/128                 trust

所以,当我这样做时 rails server 我得到了错误,然后这是我这样做的结果:

$ rails c
$ ActiveRecord::Base.connection.instance_variable_get(:@config)

2.1.5 :001 > ActiveRecord::Base.connection.instance_variable_get(:@config) PG::ConnectionBad: fe_sendauth: no password supplied

from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in initialize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:innew' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in connect' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:242:ininitialize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in new' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in postgresql_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in new_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in checkout_new_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in acquire_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in block in checkout' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in mon_synchronize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in checkout' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in block in connection' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in mon_synchronize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in retrieve_connection' ... 4 levels... from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in start' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in console' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in run_command!' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands.rb:17:in ' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in require' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:inblock in require' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in load_dependency' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:inrequire' from /home/denis/WEB/Rails/timetracker/bin/rails:8:in <top (required)>' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:inload' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in block in load' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:inload_dependency' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in load' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in require' from -e:1:in'2.1.5 :002 >

我是第一次使用 postgresql,我想我只是做错了什么。

你能帮我吗?

PostgreSQL 按照指定的顺序匹配 pg_hba.conf 个条目,因此它使用预先指定的 host all all 127.0.0.1/32 md5 条目而不是您指定的 trust 条目进行身份验证。

将您的自定义 trust 条目移至预先指定的条目上方(并可选择注释掉旧条目),然后按 运行 sudo service postgresql reload(在 Ubuntu) 或重新启动。

我对这个问题的解决方案是从 default: 组中删除 host: localhost

不知道为什么,对我来说,这条线并没有解决问题

host    mydb       myuser         127.0.0.1/32            trust

而这个是:

host    mydb       myuser         localhost               trust