Using the console

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

From within a local checkout of your app:

$ heroku console 2+2
4

This is equivalent to the Rails script/runner command. You can use it to view or edit your app’s database:

$ heroku console Author.count
12
$ heroku console 'Author.create :name => "Joe Smith"'
=> #<Author id:24 name: "Joe Smith">
Your Unix shell will escape the Ruby command. Here we've put the commands in single quotes, which are usually safe, though for complex statements you will want to use the interactive console.

Use the console to execute one-time commands like a data import:

$ heroku console 'Post.import_from_url("http://myoldblog.com/feed")'

Or, 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

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.

NOTE: The console history for each app is saved in ~/.heroku/console_history/appname. If you wish to clear the history, delete its file, or the entire directory to purge the history for all apps.