Kaptan Singh
Kaptan Singh

Reputation: 253

How to completely reset Postgres database on Heroku?

I have a learning project deployed on Heroku. It had a Postgres database provisioned. I introduced some major changes in the models of my Django project and destroyed the old database and provisioned a new one, which is totally empty, but it is not working like an empty database.

When I run the command heroku run python manage.py makemigrations, I get the error message

You are trying to add a non-nullable field....

Why am I getting this message when I have destroyed the old database?

Upvotes: 4

Views: 5421

Answers (2)

Givans Otengo
Givans Otengo

Reputation: 31

first, you need to install the Heroku CLI if you haven't then log in with the following command in your terminal:

heroku login

A browser window will open where you can authenticate your Heroku account. Once you are logged in, navigate to your Heroku app that is associated with the PostgreSQL database you want to reset:

heroku apps
heroku pg:info --app app-name

Replace app-name with the name of your Heroku app Before resetting the database, you need to disable any active connections to it

heroku maintenance:on --app your-app-name

Next, reset the database using the pg:reset command, it will reset the database to its initial state.

heroku pg:reset DATABASE_URL --app app-name

Now, you can re-create the database by running the following command:

heroku maintenance:off --app app-name

then disable maintenance mode

heroku maintenance:off --app app-name

Upvotes: 1

Chris
Chris

Reputation: 136880

First of all, you should never run manage.py makemigrations on Heroku.

By the time your code gets there no model changes should exist to generate new migrations. Run makemigrations locally to create migration files. Run migrate locally and on Heroku to apply migrations to your database.

Now that that's out of the way, this is likely caused by existing migrations files, not anything in your database. If you truly want to start over you can delete the files from each of yours apps' migrations/ directories.

Finally, there is no need to destroy and reprovision your database to reset it. Instead you can use heroku pg:reset:

The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.

Upvotes: 10

Related Questions