By user591338


2011-01-27 18:55:04 8 Comments

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

18 comments

@Shaun 2011-01-27 19:06:39

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

@user591338 2011-01-27 20:26:43

when yous say your see data what do you mean? is there a way I can add default data into the DB?

@user591338 2011-01-27 20:36:04

I'm using heroku free account and it tells me that i am not the owner of the DB

@Shaun 2011-01-27 21:12:55

@Abra: I meant 'seed data'. Also, db:drop doesn't work in heroku. Corrected instructions to use the proper beginning command.

@user591338 2011-01-27 21:36:41

what is a seed data?

@Shaun 2011-01-27 21:40:06

'Seed data' is data you have set up to be placed in your database for testing. It's usually defined in db/seeds.rb

@Jay Godse 2011-03-17 22:17:26

Thank you. Actually, "heroku pg:reset --db SHARED_DATABASE_URL" did the trick. But you got me going in the right direction.

@Phil 2011-10-18 09:40:09

@JayGodse Heroku now says SHARED_DATABASE_URL is deprecated, please use SHARED_DATABASE

@Amala 2011-12-02 23:06:02

Actually you should be checking in your schema.rb and you should use: rake db:schema:load

@quinn 2012-09-13 21:15:46

DATABASE_URL will generally work more than SHARED_DATABASE_URL.

@Haris Krajina 2013-01-23 23:48:34

They have changed it again now is heroku pg:reset DATABASE

@GangstaGraham 2013-06-08 06:08:22

To save a couple of seconds of time, use heroku pg:reset DATABASE --confirm appname

@lambinator 2013-08-21 05:51:58

For the sake of completeness: To get the value for DATABASE run heroku pg:info

@Scott Fister 2013-10-07 18:56:34

rake db:setup seems disallowed now - you'll need to run migrate and seed separately instead.

@Alan David Garcia 2014-04-07 01:46:05

They have changed it again. The command that worked for me now is heroku pg:reset DATABASE_URL

@aaron-coding 2014-12-05 00:31:38

heroku run rake db:migrate db:seed that is guaranteed to work just like rake db:setup used to work. You can chain commands like this to save time.

@Igor Kasyanchuk 2015-10-28 21:36:28

you can also try to reset DB using this gem github.com/igorkasyanchuk/rails_db, in each table view you can truncate table, or you can truncate DB using SQL

@Franklin Yu 2017-01-28 22:04:56

@AlanDavidGarcia It didn't change, it's just that the answer is misleading. The DATABASE should actually be <database>, which is a placeholder for the database name. The default database is named DATABASE_URL which you can see if you run heroku pg:info, but you can actually add more databases if you like, and manage them separately.

@superluminary 2013-02-01 18:47:16

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

heroku pg:reset DATABASE

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

@aaron-coding 2014-12-05 00:33:34

Agreed. The current way to re-populate the database is: heroku run rake db:migrate db:seed

@Charles Skariah 2017-06-22 20:22:33

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

heroku pg:reset --confirm database-name

@Ronny Kibet 2017-05-28 20:12:51

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

@Dmitriy Gusev 2017-02-03 07:17:06

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

@KIO 2016-05-12 10:51:47

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

@Luís Ramalho 2015-08-17 18:57:15

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

@RileyE 2014-06-29 02:03:55

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

@Alvin K. 2013-11-26 19:48:53

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

@Syed Ehtsham Abbas 2013-08-26 18:04:44

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

@Francisco Quintero 2018-10-21 20:04:08

Restarting might not be necessary but good point giving it a mention.

@Dave Sag 2011-09-07 06:19:25

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

@baash05 2012-02-01 22:48:57

if you just paste skip the bit after (and including) the -- heroku will tell you what to type..

@zero_cool 2014-06-07 00:50:10

useful command to get the name of your postgres database on heroku : heroku config | grep HEROKU_POSTGRESQL

@Michael Durrant 2014-06-17 10:30:49

"you use the literal text SHARED_DATABASE" ? You are using DATABASE_URL in your code samples. Do you mean that?

@Dave Sag 2014-06-17 14:34:34

Nope. I was referring to the other answer's use of the words SHARED_DATABASE

@Mark Locklear 2012-08-05 19:40:26

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

@manish_s 2013-01-06 15:53:08

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

drop schema public cascade;
create schema public;

@Flavio Wuensche 2012-10-03 21:40:48

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

@jstnno 2012-09-10 01:13:56

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

@tfwright 2012-10-11 15:29:19

For some apps you still need to use SHARED_DATABASE

@superhighfives 2012-06-09 03:11:19

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!

@Ghoti 2013-09-09 21:52:04

All the gems have been deprecated now, you need to get rid of them and install the Heroku toolbelt. toolbelt.heroku.com

@George Yacoub 2011-11-30 11:50:05

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

@Jonathan 2012-03-24 16:04:20

I prefer this answer over any else's. This works perfectly in my scenario on Heroku, I believe it is highly under voted!

@George Yacoub 2012-03-24 19:49:02

It might be because heroku changed the syntax of these commands recently.

@Jeremy Nikolai 2014-07-02 15:00:50

'heroku restart' was the one key piece I was missing. Thanks.

@Rots 2014-09-30 09:16:24

heroku run rake db:migrate no longer recreates the database

@mmarqueti 2011-08-19 16:51:34

Today the command

heroku pg:reset --db SHARED_DATABASE_URL

not working for shared plans, I'm resolve using

heroku pg:reset SHARED_DATABASE

Related Questions

Sponsored Content

34 Answered Questions

10 Answered Questions

[SOLVED] How to push different local Git branches to Heroku/master

22 Answered Questions

[SOLVED] Rails DB Migration - How To Drop a Table?

19 Answered Questions

[SOLVED] heroku - how to see all the logs

  • 2010-04-19 22:57:34
  • josh
  • 261999 View
  • 452 Score
  • 19 Answer
  • Tags:   heroku logging

7 Answered Questions

[SOLVED] How to link a folder with an existing Heroku app

  • 2011-02-26 20:39:34
  • Kevin Pang
  • 233774 View
  • 794 Score
  • 7 Answer
  • Tags:   git heroku

15 Answered Questions

19 Answered Questions

[SOLVED] Purge or recreate a Ruby on Rails database

39 Answered Questions

26 Answered Questions

[SOLVED] How can I rename a database column in a Ruby on Rails migration?

5 Answered Questions

[SOLVED] How to restart a rails server on Heroku?

Sponsored Content