Running Rake Commands
Last updated March 24, 2023
Rake tasks are executed as one-off dynos on Heroku, using the same environment as your app’s dynos. You can run Rake tasks within the remote app environment using the
heroku run rake command as follows:
$ heroku run rake db:version Running `rake db:version` attached to terminal... up, run.1 (in /home/slugs/41913_06f36ef_ab3a/mnt) Current version: 20231118092504
You can pass Rake arguments, run multiple tasks, and pass environment variables just as you would locally. For instance, to migrate the database to a specific version with verbose backtraces:
$ heroku run rake --trace db:migrate VERSION=20231118092504
After running a migration, you’ll want to restart your app with
heroku restart to reload the schema and pick up any schema changes.
Not all Rake features are supported on Heroku. The following is a list of known limitations:
Rake tasks that write to disk, such as
rake db:schema:dump, aren’t compatible with Heroku’s ephemeral filesystem.
db:resettask isn’t supported. Heroku apps don’t have permission to drop and create databases. Use the
heroku pg:resetcommand instead.
Heroku run bash
You can access the
rake command interactively through the
heroku run bash command. This command spins up a one-off dyno and give you shell access where you can run
$ heroku run bash Running bash on ⬢ polar-inlet-4930... up, run.2581 (Standard-1X) ~$ rake db:migrate --trace ** Invoke db:migrate (first_time) ** Invoke db:load_config (first_time) ** Invoke environment (first_time) ** Execute environment ...