Failing to access environment variables within `database.yml` file

23,847

Solution 1

Update: Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.


Ah, finally figured out the simple solution - it accepts embedded Ruby:

password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>

Solution 2

Short and quick solution if you are running a Rails version > 4.2 Run the following command:

spring stop

..then run rails console or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.

Previous versions of Rails didn't have this issue since they didn't use Spring server.

Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:

Within rails console:

puts ActiveRecord::Base.configurations

...or using awesome_print

ap ActiveRecord::Base.configurations

Or instead from the command line:

bin/rails runner 'puts ActiveRecord::Base.configurations'
Share:
23,847
jefflunt
Author by

jefflunt

I'm that guy who fundamentally loves to tinker and build stuff. I code because it's the single greatest creative outlet I've ever discovered, and I can't get enough of it.

Updated on July 08, 2022

Comments

  • jefflunt
    jefflunt almost 2 years

    I have the following developement section of my development.yml file:

    development:
      adapter: postgresql
      host: localhost
      database: testtb
      username: app_user
      password: ENV['APP_USER_POSTGRES_PASSWORD']     <= Troublesome line
    

    When I open a rails console via bundle exec rails console and type ENV['APP_USER_POSTGRES_PASSWORD'] I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with

    PGError FATAL:  password authentication failed for user "app_user"
    

    This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...'], but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.yml file.

    Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.yml file is being loaded?

  • rcd
    rcd over 10 years
    Great catch! Definitely works. Figaro should update their docs thanks to you.
  • Mike H-R
    Mike H-R over 10 years
    jesus christ, you don't know how long I've spent on this checking and double checking my password. thanks! I guess I should remember that YAML ain't markup language but erb is.
  • jefflunt
    jefflunt almost 10 years
    @MikeH-R - That's pretty much exactly how I felt when I finally figured this out.
  • RubyRedGrapefruit
    RubyRedGrapefruit over 8 years
    This solution doesn't work in the console on Rails 4.2.5.1. It tries to send the whole string.
  • ConstantineK
    ConstantineK about 8 years
    This is case sensitive.
  • crazy_phage
    crazy_phage about 8 years
    This is not working, I tried both upcase and downcase. for several times. I don't know what's wrong goes here, I have to hard code that to xxx.yml.
  • jefflunt
    jefflunt about 8 years
    @crazy_phage - This answer is several years old, but the ENV variable should still be available. The APP_USER_POSTGRES_PASSWORD is just the name of the environment variable I was using as an example, but yours will be whatever you set it to be. Check that the variable is set in your environment.
  • crazy_phage
    crazy_phage about 8 years
    I wrote those envs in .zshrc file, I don't if this is the cause to this probelm? ENV['what'] can not be accessed from the rails application, that's really strange.
  • FireDragon
    FireDragon almost 8 years
    I just want to add and clarify -- this used to work but it does NOT work anymore. I don't know why?? Version of Rails I assume. You can test out your database.yml file with this quick command: bin/rails runner 'puts ActiveRecord::Base.configurations' << this shows me that it's not pulling in environment vars even though I KNOW they are accessible...
  • Kroltan
    Kroltan almost 8 years
    I don't know why anyone would downvote this answer. It's perfectly valid (and also worked for me) +1
  • FireDragon
    FireDragon almost 8 years
    thank you @Kroltan, it is quite annoying how people will sometimes downvote a worthy question that actually helps others and then provide no reason why. haters gonna hate i guess. but i'm glad this helped you out.
  • crazy_phage
    crazy_phage almost 8 years
    @FireDragon Thanks a lot.
  • FireDragon
    FireDragon almost 8 years
    @crazy_phage i figured out that you have to restart spring server to reload your environment variables in newer versions of Rails that uses spring -- see my answer below if this helps
  • VAShhh
    VAShhh over 7 years
    I had issues with both rails console and rails runner and it was all because of this spring reset that is necessary in order to pick up the new env variables! thanks ;)
  • Shanthakumar
    Shanthakumar over 5 years
    so every time when a version is deployed, should this spring needs to be stopped to pick up new variables?