Sync driver — generates create_engine + sessionmaker.
Special characters in username or password (@ : / # ? %, spaces…) are URL-encoded with urllib.parse.quote.
Relative or absolute path. Use :memory: for an in-memory database.
SQLAlchemy connection strings: FAQ
What is the SQLAlchemy connection string format?
A database URL has the form dialect+driver://username:password@host:port/database,
for example postgresql+psycopg2://postgres:secret@localhost:5432/mydb.
SQLite uses a file path instead: sqlite:///./app.db.
This generator covers PostgreSQL, MySQL, MariaDB, SQLite, Oracle, SQL Server, CockroachDB, Amazon Redshift,
ClickHouse, Snowflake, IBM Db2 and DuckDB.
How do I use a password with special characters like @, : or / ?
Characters such as @ : / # ? % and spaces break the URL unless they are percent-encoded.
Encode the username and password with urllib.parse.quote(password, safe="").
Avoid quote_plus: SQLAlchemy decodes credentials with unquote, so a space encoded as
+ comes back as a literal plus sign and authentication fails.
When should I use an async driver?
Use one with asyncio applications such as FastAPI, Starlette or aiohttp. Async drivers (asyncpg, psycopg,
aiomysql, asyncmy, aiosqlite, aioodbc, oracledb) are used with create_async_engine
and async_sessionmaker instead of create_engine and sessionmaker.
Why does SQL Server need ?driver= in the URL?
pyodbc and aioodbc connect through an ODBC driver installed on the machine, and SQLAlchemy needs its exact name,
such as ?driver=ODBC+Driver+18+for+SQL+Server. The pymssql driver does not need it.
How do I keep database credentials out of my code?
Store them in a .env file that is listed in .gitignore, load it with
python-dotenv, and read each value with os.getenv.
Enable “Use Environment Variables” above to generate that setup.