SQLAlchemy: Get database name from engine


After creating an SQLALchemy engine like this

engine = create_engine('mssql+pyodbc://user:pass@dbserver:port/db_name?driver=ODBC+Driver+13+for+SQL+Server)

Is there a way to get db_name from the engine-object? I know I can parse the name from the connection string but is there a better way of doing this? I had a look at the SQLAlchemy-API but couldn’t find an answer.

Asked By: dakes



The engine provides the connection information, so you can access those parameters. For example, if you’re in the debugger, you can do:

(pdb) pp dir(dbconn.engine.url)

So the simple way to get at the database name is:

Answered By: cybertoast

To contribute to the question, I created a gist with an example:


Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.