Have environment variables from .env be picked up config/database.yml

I’m experimenting with using Heroku Postgres for my development database (no Postgres running on the local machine). I’d like to have a config/database.yml like this:

development: &default
  adapter: postgresql
  database: <%= ENV['DEVELOPMENT_DATABASE_ID'] %>
  host: <%= ENV['DEVELOPMENT_DATABASE_HOST'] %>
  min_messages: WARNING
  password: <%= ENV['DEVELOPMENT_DATABASE_PASSWORD'] %>
  port: <%= ENV['DEVELOPMENT_DATABASE_PORT'] %>
  username: <%= ENV['DEVELOPMENT_DATABASE_USER'] %>

test: &test
  <<: *default
  database: <%= ENV['TEST_DATABASE_ID'] %>
  host: <%= ENV['TEST_DATABASE_HOST'] %>
  password: <%= ENV['TEST_DATABASE_PASSWORD'] %>
  port: <%= ENV['TEST_DATABASE_PORT'] %>
  username: <%= ENV['TEST_DATABASE_USER'] %>

My first instinct is to re-use .env from Foreman. I think this works for the running server but not for rake tasks and test runs. Any thoughts on the best way to get those environment variables loaded?

Recently I have been using the figaro gem to get the job done with setting ENV variables. I used it to set up the env variable for my development database also that connects to heroku postgres

I just read about something similar on Stackoverflow… the solution was…

$ foreman run rake some_task

that way you do not have to make any changes to your workflow… =)

Dan,

Just out of curiosity… and do any of the Thoughtbot workshops cover this, i.e. ENV variables?

Seems like this would be good for beginners to know… just curious.

BTW,
have not seen any of the advanced rails workshops yet…

Foreman uses GitHub - bkeepers/dotenv: A Ruby gem to load environment variables from `.env`. under the hood to do its “loading from .env file” work so maybe tossing a Dotenv.load into the Rakefile would work. /cc @jferris

Looks like there’s also a dotenv-rails that tries to automatically do this, including for rake tasks: GitHub - bkeepers/dotenv: A Ruby gem to load environment variables from `.env`.

Probably worth trying out.