34

When I try to run:

heroku run rake db:drop db:create db:migrate

I get the error:

Running rake db:drop attached to terminal... up, run.5
Couldn't drop adsfsadfas : #<ActiveRecord::StatementInvalid: PGError: ERROR:  must be owner of database adsfsadfas
: DROP DATABASE IF EXISTS "adsfsadfas">

I am on the Heroku Cedar stack. Am I allowed to drop databases on Heroku?

Thanks!

John

John
  • 13,125
  • 14
  • 52
  • 73

2 Answers2

52

The rake db:reset task is not supported. Heroku apps do not have permission to drop and create databases. Use the heroku pg:reset command instead.

Dorian
  • 22,759
  • 8
  • 120
  • 116
Francisco Soto
  • 10,277
  • 2
  • 37
  • 46
  • 4
    yeah, you need to type `heroku pg:reset SHARED_DATABASE` then input your app name at the prompt when asked – stephenmurdoch Nov 17 '11 at 05:17
  • 20
    this has now changed to `heroku pg:reset DATABASE_URL --confirm nameofapp` - ymmv – stephenmurdoch Sep 13 '12 at 06:39
  • Note also with the (now deprecated) Heroku gem, `heroku pg:reset DATABASE_URL` will also delete your schema, despite `heroku help pg` docs indicating "Delete all data in the database". Though, in the OP's case that's what is wanted (`rake db:drop`). – Richard Michael Oct 25 '12 at 14:07
  • Joshua Schmid pointed out that you may need to also run: heroku run rake db:schema:load – jasonleonhard Sep 25 '15 at 21:19
  • `heroku pg:reset` then `heroku run rake db:migrate` (and optionally `db:fixtures:load` as that was for my staging environment) – Dorian Jul 03 '17 at 09:09
13

Directly destructive commands (drop and create) are not permitted on heroku. But if you're ok with loosing all the data, you can reset the database with pg:reset.

heroku pg:reset DATABASE_URL

Every other change should be done with db:migrate. This assures consistency of the database state.


If your migrations don't run through the full stack anymore you can use db:schema:load. This loads the schema directly from schema.rb. But be aware that this can also be destructive if create_table uses the parameters force: true or force: :cascade.

heroku run rake db:schema:load
schmijos
  • 8,114
  • 3
  • 50
  • 58