Skip to content

Instantly share code, notes, and snippets.

@amaudy
Created July 10, 2014 05:59
Show Gist options
  • Save amaudy/82cb024e9cf75c202e2b to your computer and use it in GitHub Desktop.
Save amaudy/82cb024e9cf75c202e2b to your computer and use it in GitHub Desktop.
Python+psycopg2 for drop all tables of database you given.
#!/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]
@Jwan622
Copy link

Jwan622 commented Oct 26, 2020

don't you have to commit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment