Ruby on rails Rails:fe_sendauth:Ruby没有提供密码(PG::ConnectionBad),但在Rails中可以

Ruby on rails Rails:fe_sendauth:Ruby没有提供密码(PG::ConnectionBad),但在Rails中可以,ruby-on-rails,ruby,postgresql,activerecord,pg-hba.conf,Ruby On Rails,Ruby,Postgresql,Activerecord,Pg Hba.conf,我在从straight ruby评估postgres数据库时遇到问题 我已经使用Rails创建了一个Postgres数据库 >rails new www --database=postgresql 使用Rails 4.2.5和Postgres是9.4 它生成以下config/database.yml文件 default: &default adapter: postgresql encoding: unicode pool: 5 development: <

我在从straight ruby评估postgres数据库时遇到问题

我已经使用Rails创建了一个Postgres数据库

>rails new www --database=postgresql
使用Rails 4.2.5和Postgres是9.4

它生成以下config/database.yml文件

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: www_development

test:
  <<: *default
  database: www_test

production:
  <<: *default
  database: www_production
  username: www
  password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
但是当我从非Rails项目目录运行以下app.rb时,我会收到一条fe\u sendauth:no password supplied(PG::ConnectionBad)错误消息

这显然不是Rails的问题。我要么错过了ruby中的一些东西,要么Postgres需要一个tweek来处理Rails和纯ruby之间的一些差异(我还没有意识到)。我还包括了Postgres的pg_hba.conf文件

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: www_development

test:
  <<: *default
  database: www_test

production:
  <<: *default
  database: www_production
  username: www
  password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
我绞尽脑汁想弄明白这件事。任何帮助都将不胜感激

app.rb

require 'yaml'
require 'active_record'
require 'pg'

ActiveRecord::Base.establish_connection(
  adapter:  'postgresql',
  host:     'localhost',
  database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

您在
ActiveRecord::Base中既不指定用户名,也不指定密码。请建立连接(
。我假设您想在
www\u development
数据库中使用一些不带密码的
SUPERUSER
——对吗

根据同行认证

从操作系统获取客户端的操作系统用户名 并检查它是否与请求的数据库用户名匹配

这就是为什么如果您可以
psql
而无需密码,那么您应该能够在相同的操作系统用户和环境下运行
app.rb
,而无需密码。如果您不能,那么
app.rb
将尝试使用不同的用户名连接,等等

选项:

  • username:postgres
    放入
    ActiveRecord::Base。建立连接(

  • local all peer
    更改为
    local all trust


  • 使用ENV variable作为密码,很可能变量本身不存在。请使用
    printenv
    确认是否存在。例如,在将变量包含在
    /etc/environment
    文件中后,您需要重新登录/重新启动该变量以使其可访问。如果这样做有效,可能比更改
    pg_hba.conf .

    我在设置Rails 6应用程序时遇到了同样的问题

    问题是,当使用
    rails server
    启动rails服务器并尝试从浏览器查看应用程序时,出现以下错误:

    ActiveRecord::ConnectionNotEstablished
    fe_sendauth: no password supplied
    
    以下是我解决问题的方法

    问题是由于我未指定/提供应用程序要使用的数据库密码引起的,因此当应用程序尝试连接到
    config/database.yml
    中指定的数据库时,它会遇到该错误

    我通过在
    config/database.yml
    中指定应用程序的数据库密码解决了这个问题:

    # The password associated with the postgres role (username).
    password: my-password
    
    此外,如果尚未使用以下方法创建应用程序的数据库,请确保已创建该数据库:

    rails db:create
    
    之后,我重新启动了rails服务器:

    rails server
    
    这一次,当我尝试从浏览器查看应用程序时,效果很好

    就这些

    我希望这有助于发展:
    development:
      <<: *default
      database: postgres
      username: postgres
      password: postgres
      # must specify the right DB, superuser and superuser Password as per postgreSQL setup
    
    

    谢谢Tsun。我在同一个用户下运行app.rb、rails服务器和psql。我想了解为什么我在纯ruby中得到了不同的结果。你是说rails服务器以postgres而不是运行命令的用户的身份访问数据库吗?可能是。尝试选项1。如果它解决了问题-yesWell,在用户工作时使用postgres,看起来如此很可能。我不会把这些信息涂在任何代码基础上。我会选择选项2。感谢您的帮助。很高兴@gnoll110您可以随时获取文件(
    /etc/environment
    )的源代码并导出变量,以确保该变量可用于以后启动的函数。无需注销和重新登录。