Remote Console Access
This article applies to apps on the Aspen or Bamboo stacks. For the most recent stack, Cedar, see one-off admin proceseses.
Table of Contents
Your console commands will run within the context of a dyno. This will tie up the dyno for the duration of the console command.
Heroku apps can run remote console and rake commands from the command line. This powerful feature is the primary method by which you introspect and manage your app’s database.
One-off commands
Your Unix shell will escape the Ruby command. Here we’ve put the Author.create command in single quotes, which are usually safe; for complex statements, however, you’ll want to use the interactive console.
You can use heroku console as a stand-in for Rails’s script runner, to run one-time commands directly from the command line:
$ heroku console 2+2
4
$ heroku console Author.count
12
$ heroku console 'Author.create :name => "Joe Smith"'
=> #<Author id:24 name: "Joe Smith">
This can be useful for short commands you’ll only need to use once, like a data import:
$ heroku console 'Post.import_from_url("http://myoldblog.com/feed")'
You can also use it to test that your code is working in production the way that you expect:
$ heroku console 'Geocoder.code("Dublin, Ireland")'
=> ["53.343700", "-6.249569"]
Interactive console sessions
The console history for each app is saved in ~/.heroku/console_history/app. You can delete that file to clear the history, or you can wipe the history for all your apps by removing the entire directory.
Without an argument, heroku console launches an interactive console similar to irb or the Rails script/console command:
$ heroku console
Ruby console for myapp.heroku.com
>> Post.count
=> 0
>> post = Post.create! :title => "First post", :author => "Me"
=> #<Post id:1 title: "First post" :author => "Me">
>> post.slug
=> "first_post"
>> post.destroy
Press Ctrl-D or type exit to end your console session.
The remote console runs through Heroku’s routing mesh, which means that it is subject to the same limitations that affect web requests to your app - for instance, commands that take longer than 30 seconds to run will be killed. Also, requests through the console occupy a dyno, so if you have a single-dyno app, then console use could potentially block web requests.