← Back to Upcase

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

(Dan Croak) #1

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'] %>
  min_messages: WARNING

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?

(Erik Guzman) #2

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

(Gregory Zentkovich) #3

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… =)

(Gregory Zentkovich) #4


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.

have not seen any of the advanced rails workshops yet…

(Dan Croak) #5

Foreman uses https://github.com/bkeepers/dotenv under the hood to do its “loading from .env file” work so maybe tossing a Dotenv.load into the Rakefile would work. /cc @jferris

(Joe Ferris) #6

Looks like there’s also a dotenv-rails that tries to automatically do this, including for rake tasks: https://github.com/bkeepers/dotenv#rails

Probably worth trying out.