Search code examples
ruby-on-railsrubypg

receiving "PG::ConnectionBad fe_sendauth: no password supplied" error when storing passwords in ENV variables in rails


I recently switched db's from sqlite3 to PG. My username and password for pg were hard-coded, and although it was working, this was not safe practice. SO I stored my password in an ENV variable in a .yml file, and I reference that variable in my database.yml file, but when I run the server, it gives me the error "PG::ConnectionBad fe_sendauth: no password supplied"

the following is my pg_keys.yml file

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

And the following is my database.yml file:

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'] %> 

Why is it saying that no password is supplied? Does my database.yml not see the password in the other file? Do I need to export the password?


Solution

  • It is because ENV variables come from https://github.com/bkeepers/dotenv.

    Install this gem by adding it to your Gemfile, then run bundle and set the variable PG_PASSWORD=*********** in .env in your root. Then it should work fine.