簡體   English   中英

將密碼存儲在 Rails 中的 ENV 變量中時收到“PG::ConnectionBad fe_sendauth:未提供密碼”錯誤

[英]receiving “PG::ConnectionBad fe_sendauth: no password supplied” error when storing passwords in ENV variables in rails

我最近將 db 從 sqlite3 切換到 PG。 我的 pg 用戶名和密碼是硬編碼的,雖然它可以工作,但這不是安全的做法。 所以我將密碼存儲在 a.yml 文件中的 ENV 變量中,並在我的 database.yml 文件中引用該變量,但是當我運行服務器時,它給了我錯誤“PG::ConnectionBad fe_sendauth:未提供密碼”

以下是我的 pg_keys.yml 文件

PG_PASSWORD: "***********" 

以下是我的 database.yml 文件:

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

development:
  <<: *default
  database: stockapp_development 
  username: postgres
  password: <%= ENV['PG_PASSWORD'] %>
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: stockapp_test 
  host: localhost 
  username: postgres
  password: <%= ENV['PG_PASSWORD'] %>

production:
  <<: *default
  database: stockapp_production 
  username: stockapp 
  password: <%= ENV['STOCKAPP_DATABASE_PASSWORD'] %> 

為什么說沒有提供密碼? 我的 database.yml 沒有在其他文件中看到密碼嗎? 我需要導出密碼嗎?

這是因為ENV變量來自https://github.com/bkeepers/dotenv

通過將它添加到您的Gemfile來安裝此 gem,然后運行bundle並在根目錄下的.env中設置變量PG_PASSWORD=*********** 然后它應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM