Creating staging and other environments in Rails

Ruby on Rails come with three environments by default – development, testing and production. But sooner or later one has a need for staging environment. And don’t get me wrong, you can (or should?) use the production settings there, but if you run it locally or on the same server as production, chances are you need a different database. And while you are at it, it may be handy to allow logging to console or change any other of Rails settings for that matter. In fact you can create as many other environments as you want and since it’s really easy I encourage you to do so.

To create a new environment you need to create:

  • a new config/environments/YOUR_ENVIRONMENT.rb file
  • a new database configuration entry in config/database.yml if your application uses database
  • a new secret key base entry in config/secrets.yml for apps on Rails 4.1 and higher

As I mentioned first we would need a new file in config/environments/. A short example for staging environment could be:

You might actually want to copy the production.rb environment file, but I am making it short.

To make a new entry in config/database.yml just edit the file and include a new database:

To make a new entry in config/secrets.yml you can use the following Rake command to get a new key base:

Also don’t forget on various initializers that might be configured for specific environments. For instance this might be a change you want to do for a rack-mini-profiler initializer:

As you can see some gems for development don’t assume you want to use them elsewhere.

Now you can go ahead and prepend your commands with a new RAILS_ENV values:

The same way you can then add other environments. The nice thing is that you can use same databases if you want, just different settings (asset management, logging, 3rd party services) or the same config with a different database.

Join the Conversation


  1. This stuff is great. Helped a lot as I am a beginner to Ruby on Rails. Can you also point me to some useful information on profiling a Rails application? I somehow cannot make it work.

  2. You didn’t provide any information on how to actually run a rails app in the staging environment. I would imagine that this tutorial wouldn’t be complete without how to “use” the configuration you showed how to create…

    1. I included one example of a Rails command with prepended environment variable $ RAILS_ENV=staging rake db:create, all the other Rails commands including rails s for starting the server works the same. There is no difference in using development, production or the new staging environment really. If you meant actually hosting the staging environment, then that’s a different thing (but works the same as hosting production).

Leave a comment

Your email address will not be published. Required fields are marked *