Created
July 10, 2014 05:59
-
-
Save amaudy/82cb024e9cf75c202e2b to your computer and use it in GitHub Desktop.
Python+psycopg2 for drop all tables of database you given.
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
#!/usr/bin/env python | |
import psycopg2 | |
import sys | |
""" | |
Drop all tables of database you given. | |
""" | |
try: | |
conn = psycopg2.connect("dbname='yourdbname' user='postgres' password='postgres'") | |
conn.set_isolation_level(0) | |
except: | |
print "Unable to connect to the database." | |
cur = conn.cursor() | |
try: | |
cur.execute("SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name") | |
rows = cur.fetchall() | |
for row in rows: | |
print "dropping table: ", row[1] | |
cur.execute("drop table " + row[1] + " cascade") | |
cur.close() | |
conn.close() | |
except: | |
print "Error: ", sys.exc_info()[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
don't you have to commit?