List all tables in database sample
:
from sqlalchemy import (MetaData, Table)
from sqlalchemy import create_engine, inspect
db_pass = 'xxxxxx'
db_ip='127.0.0.1'
engine = create_engine('postgresql://postgres:{}@{}/sample'.format(db_pass,db_ip))
inspector = inspect(engine)
print(inspector.get_table_names())
['pre', 'sample']
Now drop the table pre
:
metadata = MetaData()
pre_table = Table('pre', metadata)
pre_table.drop(engine, checkfirst=True)
Type \d
when to enter into psql, only one table shown in sample
database:
psql -U postgres
postgres=# \c sample
You are now connected to database "sample" as user "postgres".
sample=# \d
public | sample | table | postgres
The table pre
already dropped! But print(inspector.get_table_names())
(type the second time) still shows two tables in the database:
print(inspector.get_table_names())
['pre', 'sample']
How can clear the cache in using table.drop(engine, checkfirst=True)
?
Have you tried the inspector.clear_cache()
method?
inspector
object is backed by the PGInspector
class which inherits from reflection.Inspector
. And the later implements this clear_cache()
method.
Disclaimer: I haven't tested this. I don't have PostgresSQL setup for it. Please comment if this works.