I want to implement a function that gives information about all the tables (and their column names) that are present in a database (not only those created with SQLAlchemy). While reading the documentation it seems to me that this is done via reflection but I didn't manage to get something working. Any suggestions or examples on how to do this?
start with an engine:
from sqlalchemy import create_engine engine = create_engine("postgresql://u:p@host/database")
quick path to all table /column names, use an inspector:
from sqlalchemy import inspect inspector = inspect(engine) for table_name in inspector.get_table_names(): for column in inspector.get_columns(table_name): print("Column: %s" % column['name'])
alternatively, use MetaData / Tables:
from sqlalchemy import MetaData m = MetaData() m.reflect(engine) for table in m.tables.values(): print(table.name) for column in table.c: print(column.name)