Last active
November 10, 2020 04:48
-
-
Save harunurkst/7902bfa002fb2f00ed3520a5c518919f to your computer and use it in GitHub Desktop.
Drop all tables of postgresql with python code
This file contains hidden or 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