Skip to main content

What are Dialects?

SQLAlchemy dialects are Python modules that provide database-specific implementations for communicating with different database backends. Each dialect handles the unique characteristics, SQL syntax variations, and DBAPI driver interfaces for a specific database system.

Dialect Architecture

The dialect system consists of two main components:
  1. Dialect Layer: Handles database-specific SQL generation, type mapping, and feature support
  2. DBAPI Layer: Manages the connection to the database using Python Database API (DBAPI) drivers

Dialect URL Format

Dialects are specified using database URLs with the following format:
dialect+driver://username:password@host:port/database
The driver portion is optional. If omitted, SQLAlchemy uses the default driver for that dialect.

Supported Databases

SQLAlchemy includes built-in support for the following databases:

PostgreSQL

Popular open-source relational database with advanced features

MySQL/MariaDB

Widely-used open-source relational database systems

SQLite

Embedded, serverless SQL database engine

Oracle

Enterprise relational database management system

SQL Server

Microsoft’s enterprise database platform

Plugin System

SQLAlchemy features a plugin system that allows external dialects to be integrated using standard setuptools entry points. Third-party dialects can be:
  • Registered via setuptools entry points in setup.py
  • Registered at runtime using the registry module

Registering External Dialects

setup.py
from setuptools import setup

setup(
    name='sqlalchemy-mydialect',
    entry_points={
        'sqlalchemy.dialects': [
            'mydialect.driver = sqlalchemy_mydialect.driver:MyDialect',
        ]
    }
)

Choosing a Dialect

When selecting a dialect, consider:
FactorDescription
Database FeaturesDoes the database support the features you need (JSON, arrays, full-text search)?
Driver QualityIs the DBAPI driver actively maintained and stable?
Async SupportDo you need asynchronous database operations?
PerformanceWhat are the performance characteristics for your workload?
DeploymentWhere will the database run (cloud, on-premise, embedded)?

Dialect Configuration

Dialects can be configured with various parameters:
from sqlalchemy import create_engine

engine = create_engine(
    "postgresql+psycopg://user:pass@localhost/mydb",
    # Pool configuration
    pool_size=10,
    max_overflow=20,
    pool_recycle=3600,
    # Dialect-specific options
    connect_args={
        "options": "-c timezone=utc"
    }
)

Type System

Each dialect provides:
  • Generic types: Mapped from sqlalchemy.types (e.g., Integer, String, DateTime)
  • Dialect-specific types: Database-specific types (e.g., PostgreSQL’s JSONB, MySQL’s ENUM)

Type Mapping

Dialects maintain two key dictionaries:
  • colspecs: Maps generic types to dialect-specific implementations
  • ischema_names: Maps database type names (from reflection) to SQLAlchemy types
from sqlalchemy import Integer, String, DateTime

# Works across all databases
Column('id', Integer)
Column('name', String(50))
Column('created', DateTime)

Reflection and Introspection

Dialects provide database introspection capabilities:
from sqlalchemy import create_engine, MetaData, Table, inspect

engine = create_engine("postgresql://...")
inspector = inspect(engine)

# Get table names
tables = inspector.get_table_names()

# Reflect a table structure
metadata = MetaData()
table = Table('users', metadata, autoload_with=engine)

# Get column information
columns = inspector.get_columns('users')

Asyncio Support

Several dialects support asynchronous operations:
  • PostgreSQL: asyncpg, psycopg (async mode)
  • MySQL: aiomysql, asyncmy
  • SQLite: aiosqlite
  • Oracle: oracledb (async mode)
  • SQL Server: aioodbc
from sqlalchemy.ext.asyncio import create_async_engine

engine = create_async_engine(
    "postgresql+asyncpg://user:pass@localhost/mydb"
)
Async dialects require using SQLAlchemy’s asyncio extension and cannot be mixed with synchronous code.

Next Steps

PostgreSQL Dialect

Learn about PostgreSQL-specific features and drivers

Custom Dialects

Create your own dialect for custom databases