-
-
Save deepanshu-nickelfox/03b02d8ae5a16e239635725fe1929f69 to your computer and use it in GitHub Desktop.
Drop all tables of postgresql with python code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connections, OperationalError | |
# Drop all tables from a given database | |
""" | |
python manage.py runscript clean_database_tables | |
""" | |
def run(): | |
primary_db = connections['default'] | |
try: | |
primary_conn = primary_db.cursor() | |
except OperationalError as err: | |
print(err) | |
return None | |
try: | |
primary_conn.execute( | |
"SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name") | |
rows = primary_conn.fetchall() | |
for row in rows: | |
print("dropping table: ", row[1]) | |
primary_conn.execute("drop table " + row[1] + " cascade") | |
primary_conn.close() | |
primary_conn.close() | |
except Exception as err: | |
print(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment