60

I want to destroy the database but I'm not sure what the command would be. Does anyone know how to do this?

twe4ked
  • 2,832
  • 21
  • 24
captDaylight
  • 2,224
  • 4
  • 31
  • 42

4 Answers4

139

You shouldn't use a postgres command to fully delete your database, as you will not have permissions to create a new one. Instead you should use the heroku command to clear out your database:

heroku pg:reset DATABASE_URL
Beau
  • 11,267
  • 8
  • 44
  • 37
CraigKerstiens
  • 5,906
  • 1
  • 25
  • 28
  • What is DATABASE? I can't find the url. Im heroku there is only database name and host, user, pass. What is the database? – siamii Mar 19 '13 at 11:23
  • 19
    @siamii did it just now, the full command is `heroku pg: reset DATABASE_URL`, DATABASE_URL is an environ config and points to your db url, heroku knows how to read it, so just type it in exactly like that and it works. – Neara Apr 03 '13 at 11:48
  • If you already did do it the wrong way and lost permission to create a new database, how do you fix that? – ahnbizcad Jul 29 '14 at 20:48
  • A fine comment to protect folks from making a mistake, but not an answer to the question at all. – Yuri Gadow Oct 29 '15 at 14:28
  • 1
    I would also recommend reading http://stackoverflow.com/questions/6906715/clearing-rails-app-database-on-heroku-production-site heroku pg:reset DATABASE --confirm {app-name} is also a good command to use – Stephane Paquet Jun 08 '16 at 18:49
34

None of the answers above actually describe how to destroy a Heroku database, which was the original question (and what led me here seeking an answer).

From their docs, either of these will work:

  • heroku addons:destroy heroku-postgresql:tier (where tier is the database tier, like hobby-dev)
  • heroku addons:destroy HEROKU_POSTGRESQL_<COLOR> (if you have more than one database of that tier)

Note that because this is a destructive action it will prompt you to confirm the action. If you want to use this in a script you can skip the prompt with something like this:

heroku addons:destroy HEROKU_POSTGRESQL_<COLOR> --confirm <appname>

Hope that's helpful!

JacobEvelyn
  • 3,901
  • 1
  • 40
  • 51
  • Thanks @john-paul-ashenfelter for the edit suggestion. Not sure why your suggestions were rejected but I've made the changes you suggested. – JacobEvelyn Sep 24 '15 at 17:24
  • 1
    To create the database again, heroku addons:create heroku-postgresql:tier (where tier is the database tier, like hobby-dev). Have a look at the [reference](https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#1-provision-new-database) – UsamaMan Feb 22 '17 at 12:35
9

To answer Siamii's question above: DATABASE in heroku pg:reset DATABASE is by default postgres

Jordan Townsend
  • 101
  • 1
  • 3
7

Simply follow the steps below. Run

heroku pg:reset DATABASE

to recreate the database with nothing in it, then run

heroku run rake db:migrate

to initialize the database with the correct schema & data.

Look at the new heroku documentation it helps ;)

Andy
  • 5,287
  • 2
  • 41
  • 45
TheRealRonDez
  • 2,807
  • 2
  • 30
  • 40