Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active June 17, 2024 11:44
Show Gist options
  • Save zulhfreelancer/ea140d8ef9292fa9165e to your computer and use it in GitHub Desktop.
Save zulhfreelancer/ea140d8ef9292fa9165e to your computer and use it in GitHub Desktop.
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)

@iarobinson
Copy link

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
Copy link

RishiHQ commented Jun 24, 2020

Thank you!

@RistoLibera
Copy link

Thank you!

@teevyne
Copy link

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
Copy link

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

@bhgupta01
Copy link

Thank you for putting this together, handy and helpful resource <3

@Laranto-spb
Copy link

Thanks! It works for my demo proposes when data itself is not nessesary.

@agrberg
Copy link

agrberg commented Jul 31, 2023

If you find yourself where you cannot re-run migrations as the code has changed since they were written you can do the following to reload the schema:

  1. heroku pg:reset DATABASE
  2. heroku run rails db:environment:set RAILS_ENV=production
  3. heroku run DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:schema:load
  4. heroku run rails db:seed

Step 2 is honestly new to me but from the limited reading it sets the DB's environment to production so I assume it knows the proper database.yml values to load. Either way it's required before step 3 will run and because it is a mutation operation on a prod env database you have to manually allow it via the ENV var.

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