如何使用PostgreSQL解决Rails中的错误'fe_sendauth:没有提供密码'?

Den*_*nko 8 postgresql ruby-on-rails-4 postgresql-9.3 ubuntu-14.04

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

fe_sendauth:没有提供密码

这是我一步一步的行动:

$ sudo apt-get install postgresql postgresql-contrib
$ gem install pg
$ rails new timetracker --database=postgresql --skip-unit-test
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

这是我的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
Run Code Online (Sandbox Code Playgroud)

所以,当我这样做时,rails server我得到错误然后这就是我通过这样做得到的:

$ rails c
$ ActiveRecord::Base.connection.instance_variable_get(:@config)
Run Code Online (Sandbox Code Playgroud)

2.1.5:001> ActiveRecord :: Base.connection.instance_variable_get(:@ config)PG :: ConnectionBad:fe_sendauth:没有提供密码

来自/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'来自/home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422 :在 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 结账'来自/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'来自/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:在 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级......来自/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 来自/home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:在require' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in阻止在'from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in require'from load_dependency' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in/ 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'来自block in load' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in/home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268: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/ruby​​gems/core_ext/kernel_require.rb:54:在require' from -e:1:in'2.1.5:002>

我第一次使用postgresql,我想我只是做错了.

你能帮帮我吗?

jlj*_*one 9

我解决这个问题的方法是host: localhostdefault:组中删除.

  • 之所以可行,是因为没有使用“ host” Unix套接字。在身份验证期间,它将匹配pg_hba.conf中的peer规则,因此不需要密码(您的用户名whoa必须与数据库中的角色匹配)。 (2认同)

Col*_*Two 7

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

将自定义trust条目移动到预先指定的条目之上(并可选择注释掉旧的自定义条目),然后通过运行sudo service postgresql reload(在Ubuntu上)或重新引导来重新加载配置.