Failing to access environment variables within `database.yml` file
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'
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, 2022Comments
-
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 typeENV['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 withPGError 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 thedatabase.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 over 10 yearsGreat catch! Definitely works. Figaro should update their docs thanks to you.
-
Mike H-R over 10 yearsjesus 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 almost 10 years@MikeH-R - That's pretty much exactly how I felt when I finally figured this out.
-
RubyRedGrapefruit over 8 yearsThis solution doesn't work in the console on Rails 4.2.5.1. It tries to send the whole string.
-
ConstantineK about 8 yearsThis is case sensitive.
-
crazy_phage about 8 yearsThis 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 about 8 years@crazy_phage - This answer is several years old, but the
ENV
variable should still be available. TheAPP_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 about 8 yearsI 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 almost 8 yearsI 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 almost 8 yearsI don't know why anyone would downvote this answer. It's perfectly valid (and also worked for me) +1
-
FireDragon almost 8 yearsthank 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 almost 8 years@FireDragon Thanks a lot.
-
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 over 7 yearsI 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 over 5 yearsso every time when a version is deployed, should this spring needs to be stopped to pick up new variables?