Search code examples
ruby-on-railspostgresqlruby-on-rails-4productiondev-to-production

My rails 4.2.5 app server keeps connecting to production db instead of development db in development mode


I'm running Rails 4.2.5 app on my local machine (OS X El Capitan). It was working fine in development mode.

But then I decided to see how it works in production mode, so switched to production and run the server.

bundle install --deployment --without development test
bundle exec rake db:create RAILS_ENV=production
bundle exec rake db:migrate RAILS_ENV=production
bundle exec rake db:seed RAILS_ENV=production
bundle exec rake assets:precompile RAILS_ENV=production
bundle exec rails s -e production

Not everything looked good. So I made some changes till all are working properly.

After that, I needed to add some features implemented, so switched back to development mode again and run the server.

bundle install
bundle exec rake db:migrate
bundle exec rake db:seed
bundle exec rails s

The server run without an issue, but I found that it was working with production db instead of development db.

I tried to clear all caches by executing bundle exec rake tmp:cache:clear but it didn't help.

FYI, I'm using postgres for both production and development.

Here is the database.yml file:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 30

development:
  <<: *default
  username: <%= ENV['OATV_POSTGRES_USERNAME'] %>
  password: <%= ENV['OATV_POSTGRES_PASSWORD'] %>
  database: <%= ENV['OATV_POSTGRES_DATABASE_DEVELOPMENT'] %>

test:
  <<: *default
  username: <%= ENV['OATV_POSTGRES_USERNAME'] %>
  password: <%= ENV['OATV_POSTGRES_PASSWORD'] %>
  database: <%= ENV['OATV_POSTGRES_DATABASE_TEST'] %>

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Of course, I have set all the env vars on my local machine since I need to test in development, test and production mode.

What is the possible cause here?

Thanks for any answer in advance.


Solution

  • Just to write it here. Possible solutions:

    1.Restart the server (it should clear any unwanted variables which were set by production environment, and not cleared when running in development mode) E.g. url: <%= ENV['DATABASE_URL'] %> was set by the production environment, but wasn't set by development mode

    2.Change:

       development:
         <<: *default
    

    to:

      development:
        <<: *default
        url: postgres:///db/database-name
    

    So you would point to your local development database manually, instead of using already saved URL in production mode

    Or (maybe) third:

    3.Run bundle exec rails s -e development