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

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

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'