Is it possible to get a list of all databases in SQLAlchemy? I need a cross-database solution, so basic "SHOW DATABASES" doesn't cut it.
Similar to MetaData/Inspector capabilities to show all tables and columns.
Looks like I found the answer myself.
There is a method called get_schema_names in Inspector class, which is not very well documented, but returns list of databases (just tested with MySQL and Postgres).
Usage:
import sqlalchemy as sa
engine = sa.create_engine('mysql+pymysql://user:pwd@localhost')
insp = sa.inspect(engine)
db_list = insp.get_schema_names()
print(db_list)
engine.execute('SELECT datname FROM pg_database;').fetchall()
Every entity in DB is saved in some "system" table. For example, in Postgres you can get all DBs from pg_database table.
Here is a solution i came up with for mysql:
from sqlalchemy import create_engine
eng = create_engine('mysql+pymysql://root:password@localhost:3306', pool_recycle=3600)
q = eng.execute('SHOW DATABASES')
available_tables = q.fetchall()
It does an execute and returns all of the rows.