Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to reset PG Database on Heroku (for Rails app)?

It's important to note that running this reset will drop any existing data you have in the application

How to reset PG Database on Heroku?

  • Step 1: heroku restart
  • Step 2: heroku pg:reset DATABASE (no need to change the DATABASE)
  • Step 3: heroku run rake db:migrate
  • Step 4: heroku run rake db:seed (if you have seed)

One liner

heroku restart; heroku pg:reset DATABASE --confirm APP-NAME; heroku run rake db:migrate

Note 1

Heroku doesn't allow users from using rake db:reset, rake db:drop and rake db:create command. They only allow heroku pg:reset and rake db:migrate commands.

More info: https://devcenter.heroku.com/articles/rake

Note 2

If you have more than 1 remote, append --remote [your_remote_name] like this:

heroku run rake db:migrate --remote dev (dev is example remote here)

@jgigault

This comment has been minimized.

Copy link

@jgigault jgigault commented Jun 27, 2017

Why not this enhanced "one liner":

heroku restart && heroku pg:reset DATABASE --confirm APP-NAME && heroku run rake db:migrate

Using && instead of ; will stop the chained commands in case of a failure exit status.

Regards.

@KraevRoot

This comment has been minimized.

Copy link

@KraevRoot KraevRoot commented Jan 15, 2019

heroku restart && heroku pg:reset DATABASE_URL --confirm CHANGE_ME_TO_APP_NAME_ON_HEROKU && heroku run rake db:migrate && heroku run rake db:seed worked for me

@serodriguez68

This comment has been minimized.

Copy link

@serodriguez68 serodriguez68 commented Jan 18, 2019

What if you want your app to reset itself?

Imagine you have some type of demo that needs to reset itself once a day.

  1. Install the Database Cleaner Gem and make sure you make it available for the production environment.

  2. Create a lib/tasks/scheduler.rake file that looks similar to this

desc "This task is called by the Heroku scheduler add-on to reset the demo"
task :reset_demo => :environment do
  puts "Cleaning Up The DB..."
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean
  puts "Seeding the DB again..."
  Rake::Task["db:seed"].invoke
  puts "done!"
end
  1. Commit and do git push heroku master

  2. Add the following ENV variables to your Heroku app to disable the Database Cleaner Safeguards. You can do this by logging into your Heroku account.

DATABASE_CLEANER_ALLOW_PRODUCTION=true
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
  1. Test it by running heroku run rake reset_demo

  2. Use the Heroku Scheduler Add-on to program how often should the rake task be executed.

@florent6001

This comment has been minimized.

Copy link

@florent6001 florent6001 commented Aug 24, 2019

I have the following error :

  • [my app] has no database

but i have a cleardb database for a mysql app

@SamZapata

This comment has been minimized.

Copy link

@SamZapata SamZapata commented Oct 17, 2019

oooh thanks!

@ruvaleev

This comment has been minimized.

Copy link

@ruvaleev ruvaleev commented Apr 27, 2020

Wow, works perfect! Many thanks!

@iarobinson

This comment has been minimized.

Copy link

@iarobinson iarobinson commented May 19, 2020

It's important to note that running this reset will drop any existing data you have in the application.

@RishiHQ

This comment has been minimized.

Copy link

@RishiHQ RishiHQ commented Jun 24, 2020

Thank you!

@RistoLibera

This comment has been minimized.

Copy link

@RistoLibera RistoLibera commented Jul 13, 2021

Thank you!

@teevyne

This comment has been minimized.

Copy link

@teevyne teevyne commented Jul 22, 2021

Thank you.
This solution, in addition with some other, helped me solve my issue.
However, I lost some data in the process.
Everything else came out very fine

@Bismarck-GM

This comment has been minimized.

Copy link

@Bismarck-GM Bismarck-GM commented Aug 17, 2021

I've open this like 10 times in the last month. Super useful. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment