Engine Configuration — SQLAlchemy 1.3 Documentation

Release: 1.3.24 | Release Date: March 30, 2021

# SQLAlchemy 1.3 Documentation

# Engine Configuration

The `Engine` is the starting point for any SQLAlchemy application. It’s “home base” for the actual database and its DBAPI, delivered to the SQLAlchemy application through a connection pool and a `Dialect`, which describes how to talk to a specific kind of database/DBAPI combination.

The general structure can be illustrated as follows:

Where above, an `Engine` references both a `Dialect` and a `Pool`, which together interpret the DBAPI’s module functions as well as the behavior of the database.

Creating an engine is just a matter of issuing a single call, `create_engine()`:

    from sqlalchemy import create_engine
    engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase')

The above engine creates a `Dialect` object tailored towards PostgreSQL, as well as a `Pool` object which will establish a DBAPI connection at `localhost:5432` when a connection request is first received. Note that the `Engine` and its underlying `Pool` do not establish the first actual DBAPI connection until the `Engine.connect()` method is called, or an operation which is dependent on this method such as `Engine.execute()` is invoked. In this way, `Engine` and `Pool` can be said to have a lazy initialization behavior.

The `Engine`, once created, can either be used directly to interact with the database, or can be passed to a `Session` object to work with the ORM. This section covers the details of configuring an `Engine`. The next section, Working with Engines and Connections, will detail the usage API of the `Engine` and similar, typically for non-ORM applications.

## Supported Databases

SQLAlchemy includes many `Dialect` implementations for various backends. Dialects for the most common databases are included with SQLAlchemy; a handful of others require an additional install of a separate dialect.

See the section Dialects for information on the various backends available.

## Database Urls

The `create_engine()` function produces an `Engine` object based on a URL. These URLs follow RFC-1738, and usually can include username, password, hostname, database name as well as optional keyword arguments for additional configuration. In some cases a file path is accepted, and in others a “data source name” replaces the “host” and “database” portions. The typical form of a database URL is:

    dialect+driver://username:password@host:port/database

Dialect names include the identifying name of the SQLAlchemy dialect, a name such as `sqlite`, `mysql`, `postgresql`, `oracle`, or `mssql`. The drivername is the name of the DBAPI to be used to connect to the database using all lowercase letters. If not specified, a “default” DBAPI will be imported if available - this default is typically the most widely known driver available for that backend.

As the URL is like any other URL, special characters such as those that may be used in the password need to be URL encoded to be parsed correctly.. Below is an example of a URL that includes the password `"kx%jj5/g"`, where the percent sign and slash characters are represented as `%25` and `%2F`, respectively:

    postgresql+pg8000://dbuser:kx%25jj5%2Fg@pghost10/appdb

The encoding for the above password can be generated using urllib.parse:

    >>> import urllib.parse
    >>> urllib.parse.quote_plus("kx%jj5/g")
    'kx%25jj5%2Fg'

Examples for common connection styles follow below. For a full index of detailed information on all included dialects as well as links to third-party dialects, see Dialects.

### PostgreSQL

The PostgreSQL dialect uses psycopg2 as the default DBAPI. pg8000 is also available as a pure-Python substitute:

    # default
    engine = create_engine('postgresql://scott:tiger@localhost/mydatabase')

    # psycopg2
    engine = create_engine('postgresql+psycopg2://scott:tiger@localhost/mydatabase')

    # pg8000
    engine = create_engine('postgresql+pg8000://scott:tiger@localhost/mydatabase')

More notes on connecting to PostgreSQL at PostgreSQL.

### MySQL

The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL:

    # default
    engine = create_engine('mysql://scott:tiger@localhost/foo')

    # mysqlclient (a maintained fork of MySQL-Python)
    engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

    # PyMySQL
    engine = create_engine('mysql+pymysql://scott:tiger@localhost/foo')

More notes on connecting to MySQL at MySQL.

### Oracle

The Oracle dialect uses cx_oracle as the default DBAPI:

    engine = create_engine('oracle://scott:tiger@127.0.0.1:1521/sidname')

    engine = create_engine('oracle+cx_oracle://scott:tiger@tnsname')

More notes on connecting to Oracle at Oracle.

### Microsoft SQL Server

The SQL Server dialect uses pyodbc as the default DBAPI. pymssql is also available:

    # pyodbc
    engine = create_engine('mssql+pyodbc://scott:tiger@mydsn')

    # pymssql
    engine = create_engine('mssql+pymssql://scott:tiger@hostname:port/dbname')

More notes on connecting to SQL Server at Microsoft SQL Server.

### SQLite

SQLite connects to file-based databases, using the Python built-in module `sqlite3` by default.

As SQLite connects to local files, the URL format is slightly different. The “file” portion of the URL is the filename of the database. For a relative file path, this requires three slashes:

    # sqlite://<nohostname>/<path>
    # where <path> is relative:
    engine = create_engine('sqlite:///foo.db')

And for an absolute file path, the three slashes are followed by the absolute path:

    # Unix/Mac - 4 initial slashes in total
    engine = create_engine('sqlite:////absolute/path/to/foo.db')

    # Windows
    engine = create_engine('sqlite:///C:\\path\\to\\foo.db')

    # Windows alternative using raw string
    engine = create_engine(r'sqlite:///C:\path\to\foo.db')

To use a SQLite `:memory:` database, specify an empty URL:

    engine = create_engine('sqlite://')

More notes on connecting to SQLite at SQLite.

### Others

See Dialects, the top-level page for all additional dialect documentation.

## Engine Creation API

Object Name | Description
--- | ---
`create_engine`(*args, **kwargs) | Create a new `Engine` instance.
`engine_from_config`(configuration[, prefix], **kwargs) | Create a new Engine instance using a configuration dictionary.
`make_url`(name_or_url) | Given a string or unicode instance, produce a new URL instance.
`URL` | Represent the components of a URL used to connect to a database.

function `sqlalchemy.` `create_engine`(*args, **kwargs)

  Create a new `Engine` instance.

  The standard calling form is to send the URL as the first positional argument, usually a string that indicates database dialect and connection arguments:

      engine = create_engine("postgresql://scott:tiger@localhost/test")

  Note

  Please review Database Urls for general guidelines in composing URL strings. In particular, special characters, such as those often part of passwords, must be URL encoded to be properly parsed.

  Additional keyword arguments may then follow it which establish various options on the resulting `Engine` and its underlying `Dialect` and `Pool` constructs:

      engine = create_engine("mysql://scott:tiger@hostname/dbname",
                                  encoding='latin1', echo=True)

  The string form of the URL is `dialect[+driver]://user:password@host/dbname[?key=value..]`, where `dialect` is a database name such as `mysql`, `oracle`, `postgresql`, etc., and `driver` the name of a DBAPI, such as `psycopg2`, `pyodbc`, `cx_oracle`, etc. Alternatively, the URL can be an instance of `URL`.

  `**kwargs` takes a wide variety of options which are routed towards their appropriate components. Arguments may be specific to the `Engine`, the underlying `Dialect`, as well as the `Pool`. Specific dialects also accept keyword arguments that are unique to that dialect. Here, we describe the parameters that are common to most `create_engine()` usage.

  Once established, the newly resulting `Engine` will request a connection from the underlying `Pool` once `Engine.connect()` is called, or a method which depends on it such as `Engine.execute()` is invoked. The `Pool` in turn will establish the first actual DBAPI connection when this request is received. The `create_engine()` call itself does not establish any actual DBAPI connections directly.

  See also

  Engine Configuration

  Dialects

  Working with Engines and Connections

  Parameters

    - case_sensitive=True – if False, result column names will match in a case-insensitive fashion, that is, `row['SomeColumn']`.

    - connect_args – a dictionary of options which will be passed directly to the DBAPI’s `connect()` method as additional keyword arguments. See the example at Custom DBAPI connect() arguments / on-connect routines.

    - convert_unicode=False –

      if set to True, causes all `String` datatypes to act as though the `String.convert_unicode` flag has been set to `True`, regardless of a setting of `False` on an individual `String` type. This has the effect of causing all `String` -based columns to accommodate Python Unicode objects directly as though the datatype were the `Unicode` type.

      Deprecated since version 1.3: The `create_engine.convert_unicode` parameter is deprecated and will be removed in a future release. All modern DBAPIs now support Python Unicode directly and this parameter is unnecessary.

    - creator –

      a callable which returns a DBAPI connection. This creation function will be passed to the underlying connection pool and will be used to create all new database connections. Usage of this function causes connection parameters specified in the URL argument to be bypassed.

      This hook is not as flexible as the newer `do_connect` hook which allows complete control over how a connection is made to the database, given the full set of URL arguments and state beforehand.

      See also

      `do_connect` - event hook that allows full control over DBAPI connection mechanics.

      Custom DBAPI connect() arguments / on-connect routines

    - echo=False –

      if True, the Engine will log all statements as well as a `repr()` of their parameter lists to the default log handler, which defaults to `sys.stdout` for output. If set to the string `"debug"`, result rows will be printed to the standard output as well. The `echo` attribute of `Engine` can be modified at any time to turn logging on and off; direct control of logging is also available using the standard Python `logging` module.

      See also

      Configuring Logging - further detail on how to configure logging.

    - echo_pool=False –

      if True, the connection pool will log informational output such as when connections are invalidated as well as when connections are recycled to the default log handler, which defaults to `sys.stdout` for output. If set to the string `"debug"`, the logging will include pool checkouts and checkins. Direct control of logging is also available using the standard Python `logging` module.

      See also

      Configuring Logging - further detail on how to configure logging.

    - empty_in_strategy –

      The SQL compilation strategy to use when rendering an IN or NOT IN expression for `ColumnOperators.in_()` where the right-hand side is an empty set. This is a string value that may be one of `static`, `dynamic`, or `dynamic_warn`. The `static` strategy is the default, and an IN comparison to an empty set will generate a simple false expression “1 != 1”. The `dynamic` strategy behaves like that of SQLAlchemy 1.1 and earlier, emitting a false expression of the form “expr != expr”, which has the effect of evaluting to NULL in the case of a null expression. `dynamic_warn` is the same as `dynamic`, however also emits a warning when an empty set is encountered; this because the “dynamic” comparison is typically poorly performing on most databases.

      New in version 1.2: Added the `empty_in_strategy` setting and additionally defaulted the behavior for empty-set IN comparisons to a static boolean expression.

    - encoding –

      Defaults to `utf-8`. This is the string encoding used by SQLAlchemy for string encode/decode operations which occur within SQLAlchemy, outside of the DBAPIs own encoding facilities.

      Note

      The `encoding` parameter deals only with in-Python encoding issues that were prevalent with many DBAPIs under Python 2. Under Python 3 it is mostly unused. For DBAPIs that require client encoding configurations, such as those of MySQL and Oracle, please consult specific dialect documentation for details.

      All modern DBAPIs that work in Python 3 necessarily feature direct support for Python unicode strings. Under Python 2, this was not always the case. For those scenarios where the DBAPI is detected as not supporting a Python `unicode` object under Python 2, this encoding is used to determine the source/destination encoding. It is not used for those cases where the DBAPI handles unicode directly.

      To properly configure a system to accommodate Python `unicode` objects, the DBAPI should be configured to handle unicode to the greatest degree as is appropriate - see the notes on unicode pertaining to the specific target database in use at Dialects.

      Areas where string encoding may need to be accommodated outside of the DBAPI, nearly always under Python 2 only, include zero or more of:

      - the values passed to bound parameters, corresponding to the `Unicode` type or the `String` type when `convert_unicode` is `True`;

      - the values returned in result set columns corresponding to the `Unicode` type or the `String` type when `convert_unicode` is `True`;

      - the string SQL statement passed to the DBAPI’s `cursor.execute()` method;

      - the string names of the keys in the bound parameter dictionary passed to the DBAPI’s `cursor.execute()` as well as `cursor.setinputsizes()` methods;

      - the string column names retrieved from the DBAPI’s `cursor.description` attribute.

      When using Python 3, the DBAPI is required to support all of the above values as Python `unicode` objects, which in Python 3 are just known as `str`. In Python 2, the DBAPI does not specify unicode behavior at all, so SQLAlchemy must make decisions for each of the above values on a per-DBAPI basis - implementations are completely inconsistent in their behavior.

    - execution_options – Dictionary execution options which will be applied to all connections. See `Connection.execution_options()`

    - hide_parameters –

      Boolean, when set to True, SQL statement parameters will not be displayed in INFO logging nor will they be formatted into the string representation of `StatementError` objects.

      New in version 1.3.8.

      See also

      Configuring Logging - further detail on how to configure logging.

    - implicit_returning=True – When `True`, a RETURNING- compatible construct, if available, will be used to fetch newly generated primary key values when a single row INSERT statement is emitted with no existing returning() clause. This applies to those backends which support RETURNING or a compatible construct, including PostgreSQL, Firebird, Oracle, Microsoft SQL Server. Set this to `False` to disable the automatic usage of RETURNING.

    - isolation_level –

      this string parameter is interpreted by various dialects in order to affect the transaction isolation level of the database connection. The parameter essentially accepts some subset of these string arguments: `"SERIALIZABLE"`, `"REPEATABLE READ"`, `"READ COMMITTED"`, `"READ UNCOMMITTED"` and `"AUTOCOMMIT"`. Behavior here varies per backend, and individual dialects should be consulted directly.

      Note that the isolation level can also be set on a per-`Connection` basis as well, using the `Connection.execution_options.isolation_level` feature.

      See also

      `Connection.default_isolation_level` - view default level

      `Connection.execution_options.isolation_level` - set per `Connection` isolation level

      SQLite Transaction Isolation

      PostgreSQL Transaction Isolation

      MySQL Transaction Isolation

      Setting Transaction Isolation Levels / DBAPI AUTOCOMMIT - for the ORM

    - json_deserializer –

      for dialects that support the `JSON` datatype, this is a Python callable that will convert a JSON string to a Python object. By default, the Python `json.loads` function is used.

      Changed in version 1.3.7: The SQLite dialect renamed this from `_json_deserializer`.

    - json_serializer –

      for dialects that support the `JSON` datatype, this is a Python callable that will render a given object as JSON. By default, the Python `json.dumps` function is used.

      Changed in version 1.3.7: The SQLite dialect renamed this from `_json_serializer`.

    - label_length=None –

      optional integer value which limits the size of dynamically generated column labels to that many characters. If less than 6, labels are generated as “_(counter)”. If `None`, the value of `dialect.max_identifier_length`, which may be affected via the `create_engine.max_identifier_length` parameter, is used instead. The value of `create_engine.label_length` may not be larger than that of `create_engine.max_identfier_length`.

      See also

      `create_engine.max_identifier_length`

    - listeners – A list of one or more `PoolListener` objects which will receive connection pool events.

    - logging_name –

      String identifier which will be used within the “name” field of logging records generated within the “sqlalchemy.engine” logger. Defaults to a hexstring of the object’s id.

      See also

      Configuring Logging - further detail on how to configure logging.

    - max_identifier_length –

      integer; override the max_identifier_length determined by the dialect. if `None` or zero, has no effect. This is the database’s configured maximum number of characters that may be used in a SQL identifier such as a table name, column name, or label name. All dialects determine this value automatically, however in the case of a new database version for which this value has changed but SQLAlchemy’s dialect has not been adjusted, the value may be passed here.

      New in version 1.3.9.

      See also

      `create_engine.label_length`

    - max_overflow=10 – the number of connections to allow in connection pool “overflow”, that is connections that can be opened above and beyond the pool_size setting, which defaults to five. this is only used with `QueuePool`.

    - module=None – reference to a Python module object (the module itself, not its string name). Specifies an alternate DBAPI module to be used by the engine’s dialect. Each sub-dialect references a specific DBAPI which will be imported before first connect. This parameter causes the import to be bypassed, and the given module to be used instead. Can be used for testing of DBAPIs as well as to inject “mock” DBAPI implementations into the `Engine`.

    - paramstyle=None – The paramstyle to use when rendering bound parameters. This style defaults to the one recommended by the DBAPI itself, which is retrieved from the `.paramstyle` attribute of the DBAPI. However, most DBAPIs accept more than one paramstyle, and in particular it may be desirable to change a “named” paramstyle into a “positional” one, or vice versa. When this attribute is passed, it should be one of the values `"qmark"`, `"numeric"`, `"named"`, `"format"` or `"pyformat"`, and should correspond to a parameter style known to be supported by the DBAPI in use.

    - pool=None – an already-constructed instance of `Pool`, such as a `QueuePool` instance. If non-None, this pool will be used directly as the underlying connection pool for the engine, bypassing whatever connection parameters are present in the URL argument. For information on constructing connection pools manually, see Connection Pooling.

    - poolclass=None – a `Pool` subclass, which will be used to create a connection pool instance using the connection parameters given in the URL. Note this differs from `pool` in that you don’t actually instantiate the pool in this case, you just indicate what type of pool to be used.

    - pool_logging_name –

      String identifier which will be used within the “name” field of logging records generated within the “sqlalchemy.pool” logger. Defaults to a hexstring of the object’s id.

      See also

      Configuring Logging - further detail on how to configure logging.

    - pool_pre_ping –

      boolean, if True will enable the connection pool “pre-ping” feature that tests connections for liveness upon each checkout.

      New in version 1.2.

      See also

      Disconnect Handling - Pessimistic

    - pool_size=5 – the number of connections to keep open inside the connection pool. This used with `QueuePool` as well as `SingletonThreadPool`. With `QueuePool`, a `pool_size` setting of 0 indicates no limit; to disable pooling, set `poolclass` to `NullPool` instead.

    - pool_recycle=-1 –

      this setting causes the pool to recycle connections after the given number of seconds has passed. It defaults to -1, or no timeout. For example, setting to 3600 means connections will be recycled after one hour. Note that MySQL in particular will disconnect automatically if no activity is detected on a connection for eight hours (although this is configurable with the MySQLDB connection itself and the server configuration as well).

      See also

      Setting Pool Recycle

    - pool_reset_on_return='rollback' –

      set the `Pool.reset_on_return` parameter of the underlying `Pool` object, which can be set to the values `"rollback"`, `"commit"`, or `None`.

      See also

      `Pool.reset_on_return`

    - pool_timeout=30 – number of seconds to wait before giving up on getting a connection from the pool. This is only used with `QueuePool`.

    - pool_use_lifo=False –

      use LIFO (last-in-first-out) when retrieving connections from `QueuePool` instead of FIFO (first-in-first-out). Using LIFO, a server-side timeout scheme can reduce the number of connections used during non- peak periods of use. When planning for server-side timeouts, ensure that a recycle or pre-ping strategy is in use to gracefully handle stale connections.

      > New in version 1.3.

      > See also

      > Using FIFO vs. LIFO

      > Dealing with Disconnects

    - plugins –

      string list of plugin names to load. See `CreateEnginePlugin` for background.

      New in version 1.2.3.

    - strategy='plain' –

      selects alternate engine implementations. Currently available are:

      - the `threadlocal` strategy, which is described in Using the Threadlocal Execution Strategy;

      - the `mock` strategy, which dispatches all statement execution to a function passed as the argument `executor`. See example in the FAQ.

    - executor=None – a function taking arguments `(sql, *multiparams, **params)`, to which the `mock` strategy will dispatch all statement execution. Used only by `strategy='mock'`.

function `sqlalchemy.` `engine_from_config`(configuration, prefix='sqlalchemy.', **kwargs)

  Create a new Engine instance using a configuration dictionary.

  The dictionary is typically produced from a config file.

  The keys of interest to `engine_from_config()` should be prefixed, e.g. `sqlalchemy.url`, `sqlalchemy.echo`, etc. The ‘prefix’ argument indicates the prefix to be searched for. Each matching key (after the prefix is stripped) is treated as though it were the corresponding keyword argument to a `create_engine()` call.

  The only required key is (assuming the default prefix) `sqlalchemy.url`, which provides the database URL.

  A select set of keyword arguments will be “coerced” to their expected type based on string values. The set of arguments is extensible per-dialect using the `engine_config_types` accessor.

  Parameters

    - configuration – A dictionary (typically produced from a config file, but this is not a requirement). Items whose keys start with the value of ‘prefix’ will have that prefix stripped, and will then be passed to create_engine.

    - prefix – Prefix to match and then strip from keys in ‘configuration’.

    - kwargs – Each keyword argument to `engine_from_config()` itself overrides the corresponding item taken from the ‘configuration’ dictionary. Keyword arguments should not be prefixed.

function `sqlalchemy.engine.url.` `make_url`(name_or_url)

  Given a string or unicode instance, produce a new URL instance.

  The given string is parsed according to the RFC 1738 spec. If an existing URL object is passed, just returns the object.

class `sqlalchemy.engine.url.` `URL`(drivername, username=None, password=None, host=None, port=None, database=None, query=None)

  Represent the components of a URL used to connect to a database.

  This object is suitable to be passed directly to a `create_engine()` call. The fields of the URL are parsed from a string by the `make_url()` function. The string format of the URL is an RFC-1738-style string.

  All initialization parameters are available as public attributes.

  Parameters

    - drivername – the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in.

    - username – The user name.

    - password – database password.

    - host – The name of the host.

    - port – The port number.

    - database – The database name.

    - query – A dictionary of options to be passed to the dialect and/or the DBAPI upon connect.

  method `sqlalchemy.engine.url.URL.` `get_dialect`()

    Return the SQLAlchemy database dialect class corresponding to this URL’s driver name.

  method `sqlalchemy.engine.url.URL.` `translate_connect_args`(names=[], **kw)

    Translate url attributes into a dictionary of connection arguments.

    Returns attributes of this url (host, database, username, password, port) as a plain dictionary. The attribute names are used as the keys by default. Unset or false attributes are omitted from the final dictionary.

    Parameters

      - **kw – Optional, alternate key names for url attributes.

      - names – Deprecated. Same purpose as the keyword-based alternate names, but correlates the name to the original positionally.

## Pooling

The `Engine` will ask the connection pool for a connection when the `connect()` or `execute()` methods are called. The default connection pool, `QueuePool`, will open connections to the database on an as-needed basis. As concurrent statements are executed, `QueuePool` will grow its pool of connections to a default size of five, and will allow a default “overflow” of ten. Since the `Engine` is essentially “home base” for the connection pool, it follows that you should keep a single `Engine` per database established within an application, rather than creating a new one for each connection.

Note

`QueuePool` is not used by default for SQLite engines. See SQLite for details on SQLite connection pool usage.

For more information on connection pooling, see Connection Pooling.

## Custom DBAPI connect() arguments / on-connect routines

For cases where special connection methods are needed, in the vast majority of cases, it is most appropriate to use one of several hooks at the `create_engine()` level in order to customize this process. These are described in the following sub-sections.

### Special Keyword Arguments Passed to dbapi.connect()

All Python DBAPIs accept additional arguments beyond the basics of connecting. Common parameters include those to specify character set encodings and timeout values; more complex data includes special DBAPI constants and objects and SSL sub-parameters. There are two rudimentary means of passing these arguments without complexity.

#### Add Parameters to the URL Query string

Simple string values, as well as some numeric values and boolean flags, may be often specified in the query string of the URL directly. A common example of this is DBAPIs that accept an argument `encoding` for character encodings, such as most MySQL DBAPIs:

    engine = create_engine(
        "mysql+pymysql://user:pass@host/test?charset=utf8mb4"
    )

The advantage of using the query string is that additional DBAPI options may be specified in configuration files in a manner that’s portable to the DBAPI specified in the URL. The specific parameters passed through at this level vary by SQLAlchemy dialect. Some dialects pass all arguments through as strings, while others will parse for specific datatypes and move parameters to different places, such as into driver-level DSNs and connect strings. As per-dialect behavior in this area currently varies, the dialect documentation should be consulted for the specific dialect in use to see if particular parameters are supported at this level.

Tip

A general technique to display the exact arguments passed to the DBAPI for a given URL may be performed using the `Dialect.create_connect_args()` method directly as follows:

    >>> from sqlalchemy import create_engine
    >>> engine = create_engine("mysql+pymysql://some_user:some_pass@some_host/test?charset=utf8mb4")
    >>> args, kwargs = engine.dialect.create_connect_args(engine.url)
    >>> args, kwargs
    ([], {'host': 'some_host', 'database': 'test', 'user': 'some_user', 'password': 'some_pass', 'charset': 'utf8mb4', 'client_flag': 2})

The above `args, kwargs` pair is normally passed to the DBAPI as `dbapi.connect(*args, **kwargs)`.

#### Use the connect_args dictionary parameter

A more general system of passing any parameter to the `dbapi.connect()` function that is guaranteed to pass all parameters at all times is the `create_engine.connect_args` dictionary parameter. This may be used for parameters that are otherwise not handled by the dialect when added to the query string, as well as when special sub-structures or objects must be passed to the DBAPI. Sometimes it’s just that a particular flag must be sent as the `True` symbol and the SQLAlchemy dialect is not aware of this keyword argument to coerce it from its string form as presented in the URL. Below illustrates the use of a psycopg2 “connection factory” that replaces the underlying implementation the connection:

    engine = create_engine(
        "postgresql://user:pass@hostname/dbname",
        connect_args={"connection_factory": MyConnectionFactory}
    )

Another example is the pyodbc “timeout” parameter:

    engine = create_engine(
      "mssql+pyodbc://user:pass@sqlsrvr?driver=ODBC+Driver+13+for+SQL+Server",
      connect_args={"timeout": 30}
    )

The above example also illustrates that both URL “query string” parameters as well as `create_engine.connect_args` may be used at the same time; in the case of pyodbc, the “driver” keyword has special meaning within the URL.

### Controlling how parameters are passed to the DBAPI connect() function

Beyond manipulating the parameters passed to `connect()`, we can further customize how the DBAPI `connect()` function itself is called using the `DialectEvents.do_connect()` event hook. This hook is passed the full `*args, **kwargs` that the dialect would send to `connect()`. These collections can then be modified in place to alter how they are used:

    from sqlalchemy import event

    engine = create_engine("postgresql://user:pass@hostname/dbname")

    @event.listens_for(engine, "do_connect")
    def receive_do_connect(dialect, conn_rec, cargs, cparams):
        cparams['connection_factory'] = MyConnectionFactory

### Modifying the DBAPI connection after connect, or running commands after connect

For a DBAPI connection that SQLAlchemy creates without issue, but where we would like to modify the completed connection before it’s actually used, such as for setting special flags or running certain commands, the `PoolEvents.connect()` event hook is the most appropriate hook. This hook is called for every new connection created, before it is used by SQLAlchemy:

    from sqlalchemy import event

    engine = create_engine(
        "postgresql://user:pass@hostname/dbname"
    )

    @event.listens_for(engine, "connect")
    def connect(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("SET some session variables")
        cursor.close()

### Fully Replacing the DBAPI `connect()` function

Finally, the `DialectEvents.do_connect()` event hook can also allow us to take over the connection process entirely by establishing the connection and returning it:

    from sqlalchemy import event

    engine = create_engine(
        "postgresql://user:pass@hostname/dbname"
    )

    @event.listens_for(engine, "do_connect")
    def receive_do_connect(dialect, conn_rec, cargs, cparams):
        # return the new DBAPI connection with whatever we'd like to
        # do
        return psycopg2.connect(*cargs, **cparams)

The `DialectEvents.do_connect()` hook supersedes the previous `create_engine.creator` hook, which remains available. `DialectEvents.do_connect()` has the distinct advantage that the complete arguments parsed from the URL are also passed to the user-defined function which is not the case with `create_engine.creator`.

## Configuring Logging

Python’s standard logging module is used to implement informational and debug log output with SQLAlchemy. This allows SQLAlchemy’s logging to integrate in a standard way with other applications and libraries. There are also two parameters `create_engine.echo` and `create_engine.echo_pool` present on `create_engine()` which allow immediate logging to `sys.stdout` for the purposes of local development; these parameters ultimately interact with the regular Python loggers described below.

This section assumes familiarity with the above linked logging module. All logging performed by SQLAlchemy exists underneath the `sqlalchemy` namespace, as used by `logging.getLogger('sqlalchemy')`. When logging has been configured (i.e. such as via `logging.basicConfig()`), the general namespace of SA loggers that can be turned on is as follows:

- `sqlalchemy.engine` - controls SQL echoing. Set to `logging.INFO` for SQL query output, `logging.DEBUG` for query + result set output. These settings are equivalent to `echo=True` and `echo="debug"` on `create_engine.echo`, respectively.

- `sqlalchemy.pool` - controls connection pool logging. Set to `logging.INFO` to log connection invalidation and recycle events; set to `logging.DEBUG` to additionally log all pool checkins and checkouts. These settings are equivalent to `pool_echo=True` and `pool_echo="debug"` on `create_engine.echo_pool`, respectively.

- `sqlalchemy.dialects` - controls custom logging for SQL dialects, to the extend that logging is used within specific dialects, which is generally minimal.

- `sqlalchemy.orm` - controls logging of various ORM functions to the extent that logging is used within the ORM, which is generally minimal. Set to `logging.INFO` to log some top-level information on mapper configurations.

For example, to log SQL queries using Python logging instead of the `echo=True` flag:

    import logging

    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

By default, the log level is set to `logging.WARN` within the entire `sqlalchemy` namespace so that no log operations occur, even within an application that has logging enabled otherwise.

Note

The SQLAlchemy `Engine` conserves Python function call overhead by only emitting log statements when the current logging level is detected as `logging.INFO` or `logging.DEBUG`. It only checks this level when a new connection is procured from the connection pool. Therefore when changing the logging configuration for an already-running application, any `Connection` that’s currently active, or more commonly a `Session` object that’s active in a transaction, won’t log any SQL according to the new configuration until a new `Connection` is procured (in the case of `Session`, this is after the current transaction ends and a new one begins).

### More on the Echo Flag

As mentioned previously, the `create_engine.echo` and `create_engine.echo_pool` parameters are a shortcut to immediate logging to `sys.stdout`:

    >>> from sqlalchemy import create_engine, text
    >>> e = create_engine("sqlite://", echo=True, echo_pool='debug')
    >>> with e.connect() as conn:
    ...    print(conn.scalar(text("select 'hi'")))
    ...
    2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Created new connection <sqlite3.Connection object at 0x7f287819ac60>
    2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> checked out from pool
    2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine select 'hi'
    2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine ()
    hi
    2020-10-24 12:54:57,703 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> being returned to pool
    2020-10-24 12:54:57,704 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> rollback-on-return

Use of these flags is roughly equivalent to:

    import logging
    logging.basicConfig()
    logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
    logging.getLogger("sqlalchemy.pool").setLevel(logging.DEBUG)

It’s important to note that these two flags work independently of any existing logging configuration, and will make use of `logging.basicConfig()` unconditionally. This has the effect of being configured in addition to any existing logger configurations. Therefore, when configuring logging explicitly, ensure all echo flags are set to False at all times, to avoid getting duplicate log lines.

### Setting the Logging Name

The logger name of instance such as an `Engine` or `Pool` defaults to using a truncated hex identifier string. To set this to a specific name, use the `create_engine.logging_name` and `create_engine.pool_logging_name` with `sqlalchemy.create_engine()`:

    >>> from sqlalchemy import create_engine
    >>> from sqlalchemy import text
    >>> e = create_engine("sqlite://", echo=True, logging_name='myengine')
    >>> with e.connect() as conn:
    ...     conn.execute(text("select 'hi'"))
    ...
    2020-10-24 12:47:04,291 INFO sqlalchemy.engine.Engine.myengine select 'hi'
    2020-10-24 12:47:04,292 INFO sqlalchemy.engine.Engine.myengine ()

### Hiding Parameters

The logging emitted by `Engine` also indicates an excerpt of the SQL parameters that are present for a particular statement. To prevent these parameters from being logged for privacy purposes, enable the `create_engine.hide_parameters` flag:

    >>> e = create_engine("sqlite://", echo=True, hide_parameters=True)
    >>> with e.connect() as conn:
    ...     conn.execute(text("select :some_private_name"), {"some_private_name": "pii"})
    ...
    2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine select ?
    2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine [SQL parameters hidden due to hide_parameters=True]