user591338
user591338

Reputation:

How to empty a Heroku database

I'm working on a Ruby on Rails 3 webapp on Heroku. How do I empty the database?

Upvotes: 382

Views: 174223

Answers (18)

Shaun
Shaun

Reputation: 4799

To drop the database, if you are using SHARED_DATABASE_URL:

$ heroku pg:reset DATABASE_URL

Now to recreate the database with nothing in it:

$ heroku run rake db:migrate  

To populate the database with your seed data:

$ heroku run rake db:seed

---OR---

You can combine the last two (migrate & seed) into one action by executing this:

$ heroku run rake db:setup

Edit 2014-04-18: rake db:setup doesn't work with Rails 4, it fails with a Couldn't create database error.

Edit 2014-10-09: You can use rake db:setup with Rails 4. It does give you a Couldn't create database error (because the database was already created using the heroku pg:reset command). But it also loads your database schema and your seeds after the error message.

You can do this with pretty much any rake command, but there are exceptions. For example, db:reset doesn't work via heroku run rake. You have to use pg:reset instead.

More information can be found in Heroku's documentation:

Running Rake Commands

Reset Postgres DB

Upvotes: 727

superluminary
superluminary

Reputation: 49132

The current, ie. 2017 way to do this is:

heroku pg:reset DATABASE

https://devcenter.heroku.com/articles/heroku-postgresql#pg-reset

Upvotes: 49

Charles Skariah
Charles Skariah

Reputation: 670

If you are logged in from the console, this will do the job in the latest heroku toolbelt,

heroku pg:reset --confirm database-name

Upvotes: 1

Ronny K
Ronny K

Reputation: 3731

This is what worked for me.

1.clear db.

heroku pg:reset --app YOUR_APP

After running that you will have to type in your app name again to confirm.

2.migrate db to recreate.

heroku run rake db:migrate  --app YOUR_APP

3.add seed data to db.

heroku run rake db:seed --app YOUR_APP

Upvotes: 4

Dmitriy Gusev
Dmitriy Gusev

Reputation: 151

Best solution for you issue will be

heroku pg:reset -r heroku  --confirm your_heroku_app_name

--confirm your_heroku_app_name

is not required, but terminal always ask me do that command.

After that command you will be have pure db, without structure and stuff, after that you can run

heroku run rake db:schema:load -r heroku

or

heroku run rake db:migrate -r heroku

Upvotes: 0

Igor Kovryzhkin
Igor Kovryzhkin

Reputation: 2215

In case you prefer to use Heroku Web-site:

  1. Go to https://postgres.heroku.com/databases
  2. Select the database you want to reset
  3. Click on a settings button in the right upper corner
  4. Click "Reset Database" as shown below:
  5. type in "RESET" and press ok

heroku database reset

Upvotes: 5

Luís Ramalho
Luís Ramalho

Reputation: 10198

Now it's also possible to reset the database through their web interface.

Go to dashboard.heroku.com select your app and then you'll find the database under the add-ons category, click on it and then you can reset the database.

Reset Heroku Database

Upvotes: 8

RileyE
RileyE

Reputation: 11074

Assuming you want to reset your PostgreSQL database and set it back up, use:

heroku apps

to list your applications on Heroku. Find the name of your current application (application_name). Then run

heroku config | grep POSTGRESQL

to get the name of your databases. An example could be

HEROKU_POSTGRESQL_WHITE_URL

Finally, given application_name and database_url, you should run

heroku pg:reset `database_url` --confirm `application_name`
heroku run rake db:migrate
heroku restart

Upvotes: 3

Alvin K.
Alvin K.

Reputation: 4379

The complete answer is (for users with multi-db):

heroku pg:info - which outputs

=== HEROKU_POSTGRESQL_RED <-- this is DB
Plan Basic
Status available

heroku pg:reset HEROKU_POSTGRESQL_RED --confirm app_name

More information found in: https://devcenter.heroku.com/articles/heroku-postgresql

Upvotes: 9

Syed Ehtsham Abbas
Syed Ehtsham Abbas

Reputation: 502

Now the command is

heroku pg:reset DATABASE_URL --confirm your_app_name

this way you can specify which app's db you want to reset. Then you can run

heroku run rake db:migrate 
heroku run rake db:seed 

or direct for both above commands

heroku run rake db:setup 

And now final step to restart your app

heroku restart

Upvotes: 14

Dave Sag
Dave Sag

Reputation: 13486

Heroku has deprecated the --db option now, so now use:

heroku pg:reset DATABASE_URL --confirm {the name of your app}

It's a little confusing because you use the literal text SHARED_DATABASE but where I have written {the name of your app} substitute the name of your app. For example, if your app is called my_great_app then you use:

heroku pg:reset DATABASE_URL --confirm my_great_app

Upvotes: 103

Mark Locklear
Mark Locklear

Reputation: 5325

I always do this with the one-liner 'heroku pg:reset DATABASE'.

Upvotes: 0

Manish Singh
Manish Singh

Reputation: 6178

Login to your DB using heroku pg:psql and type the following commands:

drop schema public cascade;
create schema public;

Upvotes: 6

Flavio Wuensche
Flavio Wuensche

Reputation: 10326

Check your heroku version. I just updated mine to 2.29.0, as follows:

heroku --version
#=> heroku-gem/2.29.0 (x86_64-linux) ruby/1.9.3

Now you can run:

heroku pg:reset DATABASE --confirm YOUR_APP_NAME

Then create your database and seed it in a single command:

heroku run rake db:setup

Now restart and try your app:

heroku restart
heroku open

Upvotes: 6

jstnno
jstnno

Reputation: 1045

Now it's diffrent with heroku. Try: heroku pg:reset DATABASE --confirm

Upvotes: 8

superhighfives
superhighfives

Reputation: 442

I contacted Heroku support, and they confirmed that it is a bug with the latest gem (I am using heroku-2.26.2)

Charlie - we are aware of this issue with the 'heroku' gem and are working to fix it.

Here's the issue if you care to follow-along - https://github.com/heroku/heroku/issues/356

Downgrading to an earlier version of the 'heroku' gem should help. I've been using v2.25.0 for most of today without issue.

Downgrade with the following commands:

gem uninstall heroku

gem install heroku --version 2.25.0

If you already have multiple gems installed, you may be presented with:

Select gem to uninstall:
 1. heroku-2.25.0
 2. heroku-2.26.2
 3. All versions

Just uninstall #2 and rerun the command. Joy!

Upvotes: 11

George Yacoub
George Yacoub

Reputation: 1456

To drop the database:

$ heroku pg:reset SHARED_DATABASE --confirm NAME_OF_THE_APP

To recreate the database:

$ heroku run rake db:migrate

To seed the database:

$ heroku run rake db:seed

**Final step

$ heroku restart

Upvotes: 72

mmarqueti
mmarqueti

Reputation: 257

Today the command

heroku pg:reset --db SHARED_DATABASE_URL

not working for shared plans, I'm resolve using

heroku pg:reset SHARED_DATABASE

Upvotes: 7

Related Questions