2

I see how to drop your entire DB in heroku in Django ( Destroying a Postgres DB on Heroku). However, I want to just drop a few tables (basically perform the sql commands in 'python manage.py sqlclear appname'). How do I do this?

Community
  • 1
  • 1
fangsterr
  • 3,670
  • 4
  • 37
  • 54

2 Answers2

6

Hey this is pretty straight forward. You should just make a view function in which you can process raw SQL. I JUST did this yesterday trying to solve a postgres unique index syncing problem.

Views.py: Map this to some URL then visit the url in browser and the function will execute.

from django.db import connection, transaction

def dropTable(request):

 cursor = connection.cursor()

 cursor.execute(“DROP TABLE WHATEVER”) //custom raw SQL goes here

 success = simplejson.dumps({‘success’:’success’,}) //I do this as a success message

 return HttpResponse(success, mimetype=’application/json’) //you'll need an import or two for the json stuff to work
Hacking Life
  • 3,335
  • 2
  • 19
  • 20
  • 2
    I think you should also add the statement for committing the changes to the DB in your answer above: `transaction.commit_unless_managed()` – Arpit Rai Feb 08 '13 at 17:19
1

I had the same problem when I came across this post and I think there is an easier way to resolve this issue. Heroku rolled out PostgreSQL Studio, a web based PSQL UI that allows you to view your DB and PSQL commands.

Once you've connected to your DB, just open a worksheet and run

TRUNCATE TABLE table_name;
jaredsb
  • 11
  • 1