<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Engine Configuration — SQLAlchemy 1.2 Documentation </title> <!-- begin iterate through site-imported + sphinx environment css_files --> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../_static/docs.css" type="text/css" /> <link rel="stylesheet" href="../_static/changelog.css" type="text/css" /> <link rel="stylesheet" href="../_static/sphinx_paramlinks.css" type="text/css" /> <!-- end iterate through site-imported + sphinx environment css_files --> <!-- begin layout.mako headers --> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="SQLAlchemy 1.2 Documentation" href="../index.html" /> <link rel="up" title="Engine and Connection Use" href="engines_connections.html" /> <link rel="next" title="Working with Engines and Connections" href="connections.html" /> <link rel="prev" title="Engine and Connection Use" href="engines_connections.html" /> <!-- end layout.mako headers --> </head> <body> <div id="docs-container"> <div id="docs-top-navigation-container" class="body-background"> <div id="docs-header"> <div id="docs-version-header"> Release: <span class="version-num">1.2.19</span> | Release Date: April 15, 2019 </div> <h1>SQLAlchemy 1.2 Documentation</h1> </div> </div> <div id="docs-body-container"> <div id="fixed-sidebar" class="withsidebar"> <div id="docs-sidebar-popout"> <h3><a href="../index.html">SQLAlchemy 1.2 Documentation</a></h3> <p id="sidebar-topnav"> <a href="../contents.html">Contents</a> | <a href="../genindex.html">Index</a> </p> <div id="sidebar-search"> <form class="search" action="../search.html" method="get"> <label> Search terms: <input type="text" placeholder="search..." name="q" size="12" /> </label> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div id="docs-sidebar"> <div id="sidebar-banner"> </div> <div id="docs-sidebar-inner"> <h3> <a href="index.html" title="SQLAlchemy Core">SQLAlchemy Core</a> </h3> <ul> <li><span class="link-container"><a class="reference external" href="tutorial.html">SQL Expression Language Tutorial</a></span></li> <li><span class="link-container"><a class="reference external" href="expression_api.html">SQL Statements and Expressions API</a></span></li> <li><span class="link-container"><a class="reference external" href="schema.html">Schema Definition Language</a></span></li> <li><span class="link-container"><a class="reference external" href="types.html">Column and Data Types</a></span></li> <li><span class="link-container"><a class="reference external" href="engines_connections.html">Engine and Connection Use</a></span><ul> <li class="selected"><span class="link-container"><strong>Engine Configuration</strong><a class="paramlink headerlink reference internal" href="#">¶</a></span><ul> <li><span class="link-container"><a class="reference external" href="#supported-databases">Supported Databases</a></span></li> <li><span class="link-container"><a class="reference external" href="#database-urls">Database Urls</a></span><ul> <li><span class="link-container"><a class="reference external" href="#postgresql">PostgreSQL</a></span></li> <li><span class="link-container"><a class="reference external" href="#mysql">MySQL</a></span></li> <li><span class="link-container"><a class="reference external" href="#oracle">Oracle</a></span></li> <li><span class="link-container"><a class="reference external" href="#microsoft-sql-server">Microsoft SQL Server</a></span></li> <li><span class="link-container"><a class="reference external" href="#sqlite">SQLite</a></span></li> <li><span class="link-container"><a class="reference external" href="#others">Others</a></span></li> </ul> </li> <li><span class="link-container"><a class="reference external" href="#engine-creation-api">Engine Creation API</a></span></li> <li><span class="link-container"><a class="reference external" href="#pooling">Pooling</a></span></li> <li><span class="link-container"><a class="reference external" href="#custom-dbapi-connect-arguments">Custom DBAPI connect() arguments</a></span></li> <li><span class="link-container"><a class="reference external" href="#configuring-logging">Configuring Logging</a></span></li> </ul> </li> <li><span class="link-container"><a class="reference external" href="connections.html">Working with Engines and Connections</a></span></li> <li><span class="link-container"><a class="reference external" href="pooling.html">Connection Pooling</a></span></li> <li><span class="link-container"><a class="reference external" href="events.html">Core Events</a></span></li> </ul> </li> <li><span class="link-container"><a class="reference external" href="api_basics.html">Core API Basics</a></span></li> </ul> </div> </div> </div> <div id="docs-body" class="withsidebar" > <div class="section" id="engine-configuration"> <span id="engines-toplevel"></span><h1>Engine Configuration<a class="headerlink" href="#engine-configuration" title="Permalink to this headline">¶</a></h1> <p>The <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> is the starting point for any SQLAlchemy application. It’s “home base” for the actual database and its <a class="reference internal" href="../glossary.html#term-dbapi"><span class="xref std std-term">DBAPI</span></a>, delivered to the SQLAlchemy application through a connection pool and a <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a>, which describes how to talk to a specific kind of database/DBAPI combination.</p> <p>The general structure can be illustrated as follows:</p> <img alt="../_images/sqla_engine_arch.png" src="../_images/sqla_engine_arch.png" /> <p>Where above, an <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> references both a <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a> and a <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>, which together interpret the DBAPI’s module functions as well as the behavior of the database.</p> <p>Creating an engine is just a matter of issuing a single call, <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">create_engine</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql://scott:tiger@localhost:5432/mydatabase'</span><span class="p">)</span></pre></div> </div> <p>The above engine creates a <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a> object tailored towards PostgreSQL, as well as a <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> object which will establish a DBAPI connection at <code class="docutils literal notranslate"><span class="pre">localhost:5432</span></code> when a connection request is first received. Note that the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> and its underlying <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> do <strong>not</strong> establish the first actual DBAPI connection until the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine.connect" title="sqlalchemy.engine.Engine.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.connect()</span></code></a> method is called, or an operation which is dependent on this method such as <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine.execute" title="sqlalchemy.engine.Engine.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.execute()</span></code></a> is invoked. In this way, <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> and <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> can be said to have a <em>lazy initialization</em> behavior.</p> <p>The <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>, once created, can either be used directly to interact with the database, or can be passed to a <a class="reference internal" href="../orm/session_api.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object to work with the ORM. This section covers the details of configuring an <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>. The next section, <a class="reference internal" href="connections.html"><span class="std std-ref">Working with Engines and Connections</span></a>, will detail the usage API of the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> and similar, typically for non-ORM applications.</p> <div class="section" id="supported-databases"> <span id="supported-dbapis"></span><h2>Supported Databases<a class="headerlink" href="#supported-databases" title="Permalink to this headline">¶</a></h2> <p>SQLAlchemy includes many <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a> 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.</p> <p>See the section <a class="reference internal" href="../dialects/index.html"><span class="std std-ref">Dialects</span></a> for information on the various backends available.</p> </div> <div class="section" id="database-urls"> <span id="id1"></span><h2>Database Urls<a class="headerlink" href="#database-urls" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> function produces an <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> object based on a URL. These URLs follow <a class="reference external" href="http://rfc.net/rfc1738.html">RFC-1738</a>, 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:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">dialect</span><span class="o">+</span><span class="n">driver</span><span class="p">:</span><span class="o">//</span><span class="n">username</span><span class="p">:</span><span class="n">password</span><span class="nd">@host</span><span class="p">:</span><span class="n">port</span><span class="o">/</span><span class="n">database</span></pre></div> </div> <p>Dialect names include the identifying name of the SQLAlchemy dialect, a name such as <code class="docutils literal notranslate"><span class="pre">sqlite</span></code>, <code class="docutils literal notranslate"><span class="pre">mysql</span></code>, <code class="docutils literal notranslate"><span class="pre">postgresql</span></code>, <code class="docutils literal notranslate"><span class="pre">oracle</span></code>, or <code class="docutils literal notranslate"><span class="pre">mssql</span></code>. 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.</p> <p>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. Below is an example of a URL that includes the password <code class="docutils literal notranslate"><span class="pre">"kx%jj5/g"</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">postgresql</span><span class="o">+</span><span class="n">pg8000</span><span class="p">:</span><span class="o">//</span><span class="n">dbuser</span><span class="p">:</span><span class="n">kx</span><span class="o">%</span><span class="mi">25</span><span class="n">jj5</span><span class="o">%</span><span class="mi">2</span><span class="n">Fg</span><span class="nd">@pghost10</span><span class="o">/</span><span class="n">appdb</span></pre></div> </div> <p>The encoding for the above password can be generated using <code class="docutils literal notranslate"><span class="pre">urllib</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib.parse</span> <span class="gp">>>> </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote_plus</span><span class="p">(</span><span class="s2">"kx%jj5/g"</span><span class="p">)</span> <span class="go">'kx%25jj5%2Fg'</span></pre></div> </div> <p>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 <a class="reference internal" href="../dialects/index.html"><span class="std std-ref">Dialects</span></a>.</p> <div class="section" id="postgresql"> <h3>PostgreSQL<a class="headerlink" href="#postgresql" title="Permalink to this headline">¶</a></h3> <p>The PostgreSQL dialect uses psycopg2 as the default DBAPI. pg8000 is also available as a pure-Python substitute:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># default</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql://scott:tiger@localhost/mydatabase'</span><span class="p">)</span> <span class="c1"># psycopg2</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql+psycopg2://scott:tiger@localhost/mydatabase'</span><span class="p">)</span> <span class="c1"># pg8000</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql+pg8000://scott:tiger@localhost/mydatabase'</span><span class="p">)</span></pre></div> </div> <p>More notes on connecting to PostgreSQL at <a class="reference internal" href="../dialects/postgresql.html"><span class="std std-ref">PostgreSQL</span></a>.</p> </div> <div class="section" id="mysql"> <h3>MySQL<a class="headerlink" href="#mysql" title="Permalink to this headline">¶</a></h3> <p>The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># default</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'mysql://scott:tiger@localhost/foo'</span><span class="p">)</span> <span class="c1"># mysqlclient (a maintained fork of MySQL-Python)</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'mysql+mysqldb://scott:tiger@localhost/foo'</span><span class="p">)</span> <span class="c1"># PyMySQL</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'mysql+pymysql://scott:tiger@localhost/foo'</span><span class="p">)</span></pre></div> </div> <p>More notes on connecting to MySQL at <a class="reference internal" href="../dialects/mysql.html"><span class="std std-ref">MySQL</span></a>.</p> </div> <div class="section" id="oracle"> <h3>Oracle<a class="headerlink" href="#oracle" title="Permalink to this headline">¶</a></h3> <p>The Oracle dialect uses cx_oracle as the default DBAPI:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'oracle://scott:tiger@127.0.0.1:1521/sidname'</span><span class="p">)</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'oracle+cx_oracle://scott:tiger@tnsname'</span><span class="p">)</span></pre></div> </div> <p>More notes on connecting to Oracle at <a class="reference internal" href="../dialects/oracle.html"><span class="std std-ref">Oracle</span></a>.</p> </div> <div class="section" id="microsoft-sql-server"> <h3>Microsoft SQL Server<a class="headerlink" href="#microsoft-sql-server" title="Permalink to this headline">¶</a></h3> <p>The SQL Server dialect uses pyodbc as the default DBAPI. pymssql is also available:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># pyodbc</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'mssql+pyodbc://scott:tiger@mydsn'</span><span class="p">)</span> <span class="c1"># pymssql</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'mssql+pymssql://scott:tiger@hostname:port/dbname'</span><span class="p">)</span></pre></div> </div> <p>More notes on connecting to SQL Server at <a class="reference internal" href="../dialects/mssql.html"><span class="std std-ref">Microsoft SQL Server</span></a>.</p> </div> <div class="section" id="sqlite"> <h3>SQLite<a class="headerlink" href="#sqlite" title="Permalink to this headline">¶</a></h3> <p>SQLite connects to file-based databases, using the Python built-in module <code class="docutils literal notranslate"><span class="pre">sqlite3</span></code> by default.</p> <p>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:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># sqlite://<nohostname>/<path></span> <span class="c1"># where <path> is relative:</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'sqlite:///foo.db'</span><span class="p">)</span></pre></div> </div> <p>And for an absolute file path, the three slashes are followed by the absolute path:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Unix/Mac - 4 initial slashes in total</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'sqlite:////absolute/path/to/foo.db'</span><span class="p">)</span> <span class="c1"># Windows</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'sqlite:///C:</span><span class="se">\\</span><span class="s1">path</span><span class="se">\\</span><span class="s1">to</span><span class="se">\\</span><span class="s1">foo.db'</span><span class="p">)</span> <span class="c1"># Windows alternative using raw string</span> <span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="sa">r</span><span class="s1">'sqlite:///C:\path\to\foo.db'</span><span class="p">)</span></pre></div> </div> <p>To use a SQLite <code class="docutils literal notranslate"><span class="pre">:memory:</span></code> database, specify an empty URL:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'sqlite://'</span><span class="p">)</span></pre></div> </div> <p>More notes on connecting to SQLite at <a class="reference internal" href="../dialects/sqlite.html"><span class="std std-ref">SQLite</span></a>.</p> </div> <div class="section" id="others"> <h3>Others<a class="headerlink" href="#others" title="Permalink to this headline">¶</a></h3> <p>See <a class="reference internal" href="../dialects/index.html"><span class="std std-ref">Dialects</span></a>, the top-level page for all additional dialect documentation.</p> </div> </div> <div class="section" id="engine-creation-api"> <span id="create-engine-args"></span><h2>Engine Creation API<a class="headerlink" href="#engine-creation-api" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="sqlalchemy.create_engine"> <code class="descclassname">sqlalchemy.</code><code class="descname">create_engine</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.create_engine" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> instance.</p> <p>The standard calling form is to send the URL as the first positional argument, usually a string that indicates database dialect and connection arguments:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s2">"postgresql://scott:tiger@localhost/test"</span><span class="p">)</span></pre></div> </div> <p>Additional keyword arguments may then follow it which establish various options on the resulting <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> and its underlying <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a> and <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> constructs:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s2">"mysql://scott:tiger@hostname/dbname"</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">'latin1'</span><span class="p">,</span> <span class="n">echo</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></pre></div> </div> <p>The string form of the URL is <code class="docutils literal notranslate"><span class="pre">dialect[+driver]://user:password@host/dbname[?key=value..]</span></code>, where <code class="docutils literal notranslate"><span class="pre">dialect</span></code> is a database name such as <code class="docutils literal notranslate"><span class="pre">mysql</span></code>, <code class="docutils literal notranslate"><span class="pre">oracle</span></code>, <code class="docutils literal notranslate"><span class="pre">postgresql</span></code>, etc., and <code class="docutils literal notranslate"><span class="pre">driver</span></code> the name of a DBAPI, such as <code class="docutils literal notranslate"><span class="pre">psycopg2</span></code>, <code class="docutils literal notranslate"><span class="pre">pyodbc</span></code>, <code class="docutils literal notranslate"><span class="pre">cx_oracle</span></code>, etc. Alternatively, the URL can be an instance of <a class="reference internal" href="#sqlalchemy.engine.url.URL" title="sqlalchemy.engine.url.URL"><code class="xref py py-class docutils literal notranslate"><span class="pre">URL</span></code></a>.</p> <p><code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> takes a wide variety of options which are routed towards their appropriate components. Arguments may be specific to the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>, the underlying <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a>, as well as the <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>. Specific dialects also accept keyword arguments that are unique to that dialect. Here, we describe the parameters that are common to most <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> usage.</p> <p>Once established, the newly resulting <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> will request a connection from the underlying <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> once <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine.connect" title="sqlalchemy.engine.Engine.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.connect()</span></code></a> is called, or a method which depends on it such as <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine.execute" title="sqlalchemy.engine.Engine.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.execute()</span></code></a> is invoked. The <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> in turn will establish the first actual DBAPI connection when this request is received. The <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> call itself does <strong>not</strong> establish any actual DBAPI connections directly.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="#"><span class="doc">Engine Configuration</span></a></p> <p><a class="reference internal" href="../dialects/index.html"><span class="doc">Dialects</span></a></p> <p><a class="reference internal" href="connections.html"><span class="std std-ref">Working with Engines and Connections</span></a></p> </div> <dl class="field-list simple"> <dt class="field-odd">Parameters</dt> <dd class="field-odd"><ul class="simple"> <li><p><span class="target" id="sqlalchemy.create_engine.params.case_sensitive"></span><strong>case_sensitive=True</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.case_sensitive">¶</a> – if False, result column names will match in a case-insensitive fashion, that is, <code class="docutils literal notranslate"><span class="pre">row['SomeColumn']</span></code>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.connect_args"></span><strong>connect_args</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.connect_args">¶</a> – a dictionary of options which will be passed directly to the DBAPI’s <code class="docutils literal notranslate"><span class="pre">connect()</span></code> method as additional keyword arguments. See the example at <a class="reference internal" href="#custom-dbapi-args"><span class="std std-ref">Custom DBAPI connect() arguments</span></a>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.convert_unicode"></span><strong>convert_unicode=False</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.convert_unicode">¶</a> – <p>if set to True, causes all <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> datatypes to act as though the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String.params.convert_unicode" title="sqlalchemy.types.String"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">String.convert_unicode</span></code></a> flag has been set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, regardless of a setting of <code class="docutils literal notranslate"><span class="pre">False</span></code> on an individual <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> type. This has the effect of causing all <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> -based columns to accommodate Python Unicode objects directly as though the datatype were the <a class="reference internal" href="type_basics.html#sqlalchemy.types.Unicode" title="sqlalchemy.types.Unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">Unicode</span></code></a> type.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>SQLAlchemy’s unicode-conversion flags and features only apply to Python 2; in Python 3, all string objects are Unicode objects. For this reason, as well as the fact that virtually all modern DBAPIs now support Unicode natively even under Python 2, the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine.params.convert_unicode" title="sqlalchemy.engine.Engine"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Engine.convert_unicode</span></code></a> flag is inherently a legacy feature.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This flag does <strong>not</strong> imply that SQLAlchemy’s unicode-conversion services will be used, as all modern DBAPIs already handle unicode natively; in most cases it only indicates that the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> datatype will return Python unicode objects, rather than plain strings. The <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> datatype itself has additional options to force the usage of SQLAlchemy’s unicode converters.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This flag does <strong>not</strong> impact “raw” SQL statements that have no typing information set up; that is, if the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> datatype is not used, no unicode behavior is implied.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="type_basics.html#sqlalchemy.types.String.params.convert_unicode" title="sqlalchemy.types.String"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">String.convert_unicode</span></code></a> - the flag local to the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> datatype has additional options which can force unicode handling on a per-type basis.</p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.creator"></span><strong>creator</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.creator">¶</a> – 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.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.echo"></span><strong>echo=False</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.echo">¶</a> – <p>if True, the Engine will log all statements as well as a <code class="docutils literal notranslate"><span class="pre">repr()</span></code> of their parameter lists to the default log handler, which defaults to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> for output. If set to the string <code class="docutils literal notranslate"><span class="pre">"debug"</span></code>, result rows will be printed to the standard output as well. The <code class="docutils literal notranslate"><span class="pre">echo</span></code> attribute of <code class="docutils literal notranslate"><span class="pre">Engine</span></code> can be modified at any time to turn logging on and off; direct control of logging is also available using the standard Python <code class="docutils literal notranslate"><span class="pre">logging</span></code> module.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="#dbengine-logging"><span class="std std-ref">Configuring Logging</span></a> - further detail on how to configure logging.</p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.echo_pool"></span><strong>echo_pool=False</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.echo_pool">¶</a> – <p>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 <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> for output. If set to the string <code class="docutils literal notranslate"><span class="pre">"debug"</span></code>, the logging will include pool checkouts and checkins. Direct control of logging is also available using the standard Python <code class="docutils literal notranslate"><span class="pre">logging</span></code> module.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="#dbengine-logging"><span class="std std-ref">Configuring Logging</span></a> - further detail on how to configure logging.</p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.empty_in_strategy"></span><strong>empty_in_strategy</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.empty_in_strategy">¶</a> – <p>The SQL compilation strategy to use when rendering an IN or NOT IN expression for <a class="reference internal" href="sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.in_" title="sqlalchemy.sql.operators.ColumnOperators.in_"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ColumnOperators.in_()</span></code></a> where the right-hand side is an empty set. This is a string value that may be one of <code class="docutils literal notranslate"><span class="pre">static</span></code>, <code class="docutils literal notranslate"><span class="pre">dynamic</span></code>, or <code class="docutils literal notranslate"><span class="pre">dynamic_warn</span></code>. The <code class="docutils literal notranslate"><span class="pre">static</span></code> strategy is the default, and an IN comparison to an empty set will generate a simple false expression “1 != 1”. The <code class="docutils literal notranslate"><span class="pre">dynamic</span></code> 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. <code class="docutils literal notranslate"><span class="pre">dynamic_warn</span></code> is the same as <code class="docutils literal notranslate"><span class="pre">dynamic</span></code>, however also emits a warning when an empty set is encountered; this because the “dynamic” comparison is typically poorly performing on most databases.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 1.2: </span>Added the <code class="docutils literal notranslate"><span class="pre">empty_in_strategy</span></code> setting and additionally defaulted the behavior for empty-set IN comparisons to a static boolean expression.</p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.encoding"></span><strong>encoding</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.encoding">¶</a> – <p>Defaults to <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>. This is the string encoding used by SQLAlchemy for string encode/decode operations which occur within SQLAlchemy, <strong>outside of the DBAPI.</strong> Most modern DBAPIs feature some degree of direct support for Python <code class="docutils literal notranslate"><span class="pre">unicode</span></code> objects, what you see in Python 2 as a string of the form <code class="docutils literal notranslate"><span class="pre">u'some</span> <span class="pre">string'</span></code>. For those scenarios where the DBAPI is detected as not supporting a Python <code class="docutils literal notranslate"><span class="pre">unicode</span></code> object, this encoding is used to determine the source/destination encoding. It is <strong>not used</strong> for those cases where the DBAPI handles unicode directly.</p> <p>To properly configure a system to accommodate Python <code class="docutils literal notranslate"><span class="pre">unicode</span></code> 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 <a class="reference internal" href="../dialects/index.html"><span class="std std-ref">Dialects</span></a>.</p> <p>Areas where string encoding may need to be accommodated outside of the DBAPI include zero or more of:</p> <ul> <li><p>the values passed to bound parameters, corresponding to the <a class="reference internal" href="type_basics.html#sqlalchemy.types.Unicode" title="sqlalchemy.types.Unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">Unicode</span></code></a> type or the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> type when <code class="docutils literal notranslate"><span class="pre">convert_unicode</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>;</p></li> <li><p>the values returned in result set columns corresponding to the <a class="reference internal" href="type_basics.html#sqlalchemy.types.Unicode" title="sqlalchemy.types.Unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">Unicode</span></code></a> type or the <a class="reference internal" href="type_basics.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><code class="xref py py-class docutils literal notranslate"><span class="pre">String</span></code></a> type when <code class="docutils literal notranslate"><span class="pre">convert_unicode</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>;</p></li> <li><p>the string SQL statement passed to the DBAPI’s <code class="docutils literal notranslate"><span class="pre">cursor.execute()</span></code> method;</p></li> <li><p>the string names of the keys in the bound parameter dictionary passed to the DBAPI’s <code class="docutils literal notranslate"><span class="pre">cursor.execute()</span></code> as well as <code class="docutils literal notranslate"><span class="pre">cursor.setinputsizes()</span></code> methods;</p></li> <li><p>the string column names retrieved from the DBAPI’s <code class="docutils literal notranslate"><span class="pre">cursor.description</span></code> attribute.</p></li> </ul> <p>When using Python 3, the DBAPI is required to support <em>all</em> of the above values as Python <code class="docutils literal notranslate"><span class="pre">unicode</span></code> objects, which in Python 3 are just known as <code class="docutils literal notranslate"><span class="pre">str</span></code>. 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.</p> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.execution_options"></span><strong>execution_options</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.execution_options">¶</a> – Dictionary execution options which will be applied to all connections. See <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection.execution_options" title="sqlalchemy.engine.Connection.execution_options"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execution_options()</span></code></a></p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.implicit_returning"></span><strong>implicit_returning=True</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.implicit_returning">¶</a> – When <code class="docutils literal notranslate"><span class="pre">True</span></code>, 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 <code class="docutils literal notranslate"><span class="pre">False</span></code> to disable the automatic usage of RETURNING.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.isolation_level"></span><strong>isolation_level</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.isolation_level">¶</a> – <p>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: <code class="docutils literal notranslate"><span class="pre">"SERIALIZABLE"</span></code>, <code class="docutils literal notranslate"><span class="pre">"REPEATABLE_READ"</span></code>, <code class="docutils literal notranslate"><span class="pre">"READ_COMMITTED"</span></code>, <code class="docutils literal notranslate"><span class="pre">"READ_UNCOMMITTED"</span></code> and <code class="docutils literal notranslate"><span class="pre">"AUTOCOMMIT"</span></code>. Behavior here varies per backend, and individual dialects should be consulted directly.</p> <p>Note that the isolation level can also be set on a per-<a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> basis as well, using the <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection.execution_options.params.isolation_level" title="sqlalchemy.engine.Connection.execution_options"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Connection.execution_options.isolation_level</span></code></a> feature.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="connections.html#sqlalchemy.engine.Connection.default_isolation_level" title="sqlalchemy.engine.Connection.default_isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.default_isolation_level</span></code></a> - view default level</p> <p><a class="reference internal" href="connections.html#sqlalchemy.engine.Connection.execution_options.params.isolation_level" title="sqlalchemy.engine.Connection.execution_options"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Connection.execution_options.isolation_level</span></code></a> - set per <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> isolation level</p> <p><a class="reference internal" href="../dialects/sqlite.html#sqlite-isolation-level"><span class="std std-ref">SQLite Transaction Isolation</span></a></p> <p><a class="reference internal" href="../dialects/postgresql.html#postgresql-isolation-level"><span class="std std-ref">PostgreSQL Transaction Isolation</span></a></p> <p><a class="reference internal" href="../dialects/mysql.html#mysql-isolation-level"><span class="std std-ref">MySQL Transaction Isolation</span></a></p> <p><a class="reference internal" href="../orm/session_transaction.html#session-transaction-isolation"><span class="std std-ref">Setting Transaction Isolation Levels</span></a> - for the ORM</p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.label_length"></span><strong>label_length=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.label_length">¶</a> – 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 <code class="docutils literal notranslate"><span class="pre">None</span></code>, the value of <code class="docutils literal notranslate"><span class="pre">dialect.max_identifier_length</span></code> is used instead.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.listeners"></span><strong>listeners</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.listeners">¶</a> – A list of one or more <a class="reference internal" href="interfaces.html#sqlalchemy.interfaces.PoolListener" title="sqlalchemy.interfaces.PoolListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">PoolListener</span></code></a> objects which will receive connection pool events.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.logging_name"></span><strong>logging_name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.logging_name">¶</a> – 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.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.max_overflow"></span><strong>max_overflow=10</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.max_overflow">¶</a> – 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 <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.module"></span><strong>module=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.module">¶</a> – 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 <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.paramstyle"></span><strong>paramstyle=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.paramstyle">¶</a> – The <a class="reference external" href="http://legacy.python.org/dev/peps/pep-0249/#paramstyle">paramstyle</a> to use when rendering bound parameters. This style defaults to the one recommended by the DBAPI itself, which is retrieved from the <code class="docutils literal notranslate"><span class="pre">.paramstyle</span></code> 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 <code class="docutils literal notranslate"><span class="pre">"qmark"</span></code>, <code class="docutils literal notranslate"><span class="pre">"numeric"</span></code>, <code class="docutils literal notranslate"><span class="pre">"named"</span></code>, <code class="docutils literal notranslate"><span class="pre">"format"</span></code> or <code class="docutils literal notranslate"><span class="pre">"pyformat"</span></code>, and should correspond to a parameter style known to be supported by the DBAPI in use.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool"></span><strong>pool=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool">¶</a> – an already-constructed instance of <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a>, such as a <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a> 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 <a class="reference internal" href="pooling.html"><span class="std std-ref">Connection Pooling</span></a>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.poolclass"></span><strong>poolclass=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.poolclass">¶</a> – a <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> subclass, which will be used to create a connection pool instance using the connection parameters given in the URL. Note this differs from <code class="docutils literal notranslate"><span class="pre">pool</span></code> in that you don’t actually instantiate the pool in this case, you just indicate what type of pool to be used.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_logging_name"></span><strong>pool_logging_name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_logging_name">¶</a> – 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.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_pre_ping"></span><strong>pool_pre_ping</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_pre_ping">¶</a> – <p>boolean, if True will enable the connection pool “pre-ping” feature that tests connections for liveness upon each checkout.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 1.2.</span></p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="pooling.html#pool-disconnects-pessimistic"><span class="std std-ref">Disconnect Handling - Pessimistic</span></a></p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_size"></span><strong>pool_size=5</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_size">¶</a> – the number of connections to keep open inside the connection pool. This used with <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a> as well as <a class="reference internal" href="pooling.html#sqlalchemy.pool.SingletonThreadPool" title="sqlalchemy.pool.SingletonThreadPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">SingletonThreadPool</span></code></a>. With <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a>, a <code class="docutils literal notranslate"><span class="pre">pool_size</span></code> setting of 0 indicates no limit; to disable pooling, set <code class="docutils literal notranslate"><span class="pre">poolclass</span></code> to <a class="reference internal" href="pooling.html#sqlalchemy.pool.NullPool" title="sqlalchemy.pool.NullPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">NullPool</span></code></a> instead.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_recycle"></span><strong>pool_recycle=-1</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_recycle">¶</a> – <p>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).</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="pooling.html#pool-setting-recycle"><span class="std std-ref">Setting Pool Recycle</span></a></p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_reset_on_return"></span><strong>pool_reset_on_return='rollback'</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_reset_on_return">¶</a> – <p>set the <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool.params.reset_on_return" title="sqlalchemy.pool.Pool"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Pool.reset_on_return</span></code></a> parameter of the underlying <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> object, which can be set to the values <code class="docutils literal notranslate"><span class="pre">"rollback"</span></code>, <code class="docutils literal notranslate"><span class="pre">"commit"</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool.params.reset_on_return" title="sqlalchemy.pool.Pool"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Pool.reset_on_return</span></code></a></p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.pool_timeout"></span><strong>pool_timeout=30</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.pool_timeout">¶</a> – number of seconds to wait before giving up on getting a connection from the pool. This is only used with <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a>.</p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.plugins"></span><strong>plugins</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.plugins">¶</a> – <p>string list of plugin names to load. See <a class="reference internal" href="connections.html#sqlalchemy.engine.CreateEnginePlugin" title="sqlalchemy.engine.CreateEnginePlugin"><code class="xref py py-class docutils literal notranslate"><span class="pre">CreateEnginePlugin</span></code></a> for background.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 1.2.3.</span></p> </div> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.strategy"></span><strong>strategy='plain'</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.strategy">¶</a> – <p>selects alternate engine implementations. Currently available are:</p> <ul> <li><p>the <code class="docutils literal notranslate"><span class="pre">threadlocal</span></code> strategy, which is described in <a class="reference internal" href="connections.html#threadlocal-strategy"><span class="std std-ref">Using the Threadlocal Execution Strategy</span></a>;</p></li> <li><p>the <code class="docutils literal notranslate"><span class="pre">mock</span></code> strategy, which dispatches all statement execution to a function passed as the argument <code class="docutils literal notranslate"><span class="pre">executor</span></code>. See <a class="reference external" href="http://docs.sqlalchemy.org/en/latest/faq/metadata_schema.html#how-can-i-get-the-create-table-drop-table-output-as-a-string">example in the FAQ</a>.</p></li> </ul> </p></li> <li><p><span class="target" id="sqlalchemy.create_engine.params.executor"></span><strong>executor=None</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.create_engine.params.executor">¶</a> – a function taking arguments <code class="docutils literal notranslate"><span class="pre">(sql,</span> <span class="pre">*multiparams,</span> <span class="pre">**params)</span></code>, to which the <code class="docutils literal notranslate"><span class="pre">mock</span></code> strategy will dispatch all statement execution. Used only by <code class="docutils literal notranslate"><span class="pre">strategy='mock'</span></code>.</p></li> </ul> </dd> </dl> </dd></dl> <dl class="function"> <dt id="sqlalchemy.engine_from_config"> <code class="descclassname">sqlalchemy.</code><code class="descname">engine_from_config</code><span class="sig-paren">(</span><em>configuration</em>, <em>prefix='sqlalchemy.'</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.engine_from_config" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new Engine instance using a configuration dictionary.</p> <p>The dictionary is typically produced from a config file.</p> <p>The keys of interest to <code class="docutils literal notranslate"><span class="pre">engine_from_config()</span></code> should be prefixed, e.g. <code class="docutils literal notranslate"><span class="pre">sqlalchemy.url</span></code>, <code class="docutils literal notranslate"><span class="pre">sqlalchemy.echo</span></code>, 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 <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> call.</p> <p>The only required key is (assuming the default prefix) <code class="docutils literal notranslate"><span class="pre">sqlalchemy.url</span></code>, which provides the <a class="reference internal" href="#database-urls"><span class="std std-ref">database URL</span></a>.</p> <p>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 <code class="docutils literal notranslate"><span class="pre">engine_config_types</span></code> accessor.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters</dt> <dd class="field-odd"><ul class="simple"> <li><p><span class="target" id="sqlalchemy.engine_from_config.params.configuration"></span><strong>configuration</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine_from_config.params.configuration">¶</a> – 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 <span class="xref std std-ref">create_engine</span>.</p></li> <li><p><span class="target" id="sqlalchemy.engine_from_config.params.prefix"></span><strong>prefix</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine_from_config.params.prefix">¶</a> – Prefix to match and then strip from keys in ‘configuration’.</p></li> <li><p><span class="target" id="sqlalchemy.engine_from_config.params.kwargs"></span><strong>kwargs</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine_from_config.params.kwargs">¶</a> – Each keyword argument to <code class="docutils literal notranslate"><span class="pre">engine_from_config()</span></code> itself overrides the corresponding item taken from the ‘configuration’ dictionary. Keyword arguments should <em>not</em> be prefixed.</p></li> </ul> </dd> </dl> </dd></dl> <dl class="function"> <dt id="sqlalchemy.engine.url.make_url"> <code class="descclassname">sqlalchemy.engine.url.</code><code class="descname">make_url</code><span class="sig-paren">(</span><em>name_or_url</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.engine.url.make_url" title="Permalink to this definition">¶</a></dt> <dd><p>Given a string or unicode instance, produce a new URL instance.</p> <p>The given string is parsed according to the RFC 1738 spec. If an existing URL object is passed, just returns the object.</p> </dd></dl> <dl class="class"> <dt id="sqlalchemy.engine.url.URL"> <em class="property">class </em><code class="descclassname">sqlalchemy.engine.url.</code><code class="descname">URL</code><span class="sig-paren">(</span><em>drivername</em>, <em>username=None</em>, <em>password=None</em>, <em>host=None</em>, <em>port=None</em>, <em>database=None</em>, <em>query=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.engine.url.URL" title="Permalink to this definition">¶</a></dt> <dd><p>Represent the components of a URL used to connect to a database.</p> <p>This object is suitable to be passed directly to a <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> call. The fields of the URL are parsed from a string by the <a class="reference internal" href="#sqlalchemy.engine.url.make_url" title="sqlalchemy.engine.url.make_url"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_url()</span></code></a> function. the string format of the URL is an RFC-1738-style string.</p> <p>All initialization parameters are available as public attributes.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters</dt> <dd class="field-odd"><ul class="simple"> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.drivername"></span><strong>drivername</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.drivername">¶</a> – the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.username"></span><strong>username</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.username">¶</a> – The user name.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.password"></span><strong>password</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.password">¶</a> – database password.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.host"></span><strong>host</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.host">¶</a> – The name of the host.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.port"></span><strong>port</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.port">¶</a> – The port number.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.database"></span><strong>database</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.database">¶</a> – The database name.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.params.query"></span><strong>query</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.params.query">¶</a> – A dictionary of options to be passed to the dialect and/or the DBAPI upon connect.</p></li> </ul> </dd> </dl> <dl class="method"> <dt id="sqlalchemy.engine.url.URL.get_dialect"> <code class="descname">get_dialect</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.engine.url.URL.get_dialect" title="Permalink to this definition">¶</a></dt> <dd><p>Return the SQLAlchemy database dialect class corresponding to this URL’s driver name.</p> </dd></dl> <dl class="method"> <dt id="sqlalchemy.engine.url.URL.translate_connect_args"> <code class="descname">translate_connect_args</code><span class="sig-paren">(</span><em>names=[]</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.engine.url.URL.translate_connect_args" title="Permalink to this definition">¶</a></dt> <dd><p>Translate url attributes into a dictionary of connection arguments.</p> <p>Returns attributes of this url (<cite>host</cite>, <cite>database</cite>, <cite>username</cite>, <cite>password</cite>, <cite>port</cite>) as a plain dictionary. The attribute names are used as the keys by default. Unset or false attributes are omitted from the final dictionary.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters</dt> <dd class="field-odd"><ul class="simple"> <li><p><span class="target" id="sqlalchemy.engine.url.URL.translate_connect_args.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.translate_connect_args.params.**kw">¶</a> – Optional, alternate key names for url attributes.</p></li> <li><p><span class="target" id="sqlalchemy.engine.url.URL.translate_connect_args.params.names"></span><strong>names</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.engine.url.URL.translate_connect_args.params.names">¶</a> – Deprecated. Same purpose as the keyword-based alternate names, but correlates the name to the original positionally.</p></li> </ul> </dd> </dl> </dd></dl> </dd></dl> </div> <div class="section" id="pooling"> <h2>Pooling<a class="headerlink" href="#pooling" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> will ask the connection pool for a connection when the <code class="docutils literal notranslate"><span class="pre">connect()</span></code> or <code class="docutils literal notranslate"><span class="pre">execute()</span></code> methods are called. The default connection pool, <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a>, will open connections to the database on an as-needed basis. As concurrent statements are executed, <a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a> will grow its pool of connections to a default size of five, and will allow a default “overflow” of ten. Since the <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> is essentially “home base” for the connection pool, it follows that you should keep a single <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> per database established within an application, rather than creating a new one for each connection.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="pooling.html#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueuePool</span></code></a> is not used by default for SQLite engines. See <a class="reference internal" href="../dialects/sqlite.html"><span class="std std-ref">SQLite</span></a> for details on SQLite connection pool usage.</p> </div> <p>For more information on connection pooling, see <a class="reference internal" href="pooling.html"><span class="std std-ref">Connection Pooling</span></a>.</p> </div> <div class="section" id="custom-dbapi-connect-arguments"> <span id="custom-dbapi-args"></span><h2>Custom DBAPI connect() arguments<a class="headerlink" href="#custom-dbapi-connect-arguments" title="Permalink to this headline">¶</a></h2> <p>Custom arguments used when issuing the <code class="docutils literal notranslate"><span class="pre">connect()</span></code> call to the underlying DBAPI may be issued in three distinct ways. String-based arguments can be passed directly from the URL string as query arguments:</p> <div class="highlight-python+sql notranslate"><div class="highlight"><pre><span></span><span class="n">db</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql://scott:tiger@localhost/test?argument1=foo&argument2=bar'</span><span class="p">)</span></pre></div> </div> <p>If SQLAlchemy’s database connector is aware of a particular query argument, it may convert its type from string to its proper type.</p> <p><a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> also takes an argument <code class="docutils literal notranslate"><span class="pre">connect_args</span></code> which is an additional dictionary that will be passed to <code class="docutils literal notranslate"><span class="pre">connect()</span></code>. This can be used when arguments of a type other than string are required, and SQLAlchemy’s database connector has no type conversion logic present for that parameter:</p> <div class="highlight-python+sql notranslate"><div class="highlight"><pre><span></span><span class="n">db</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql://scott:tiger@localhost/test'</span><span class="p">,</span> <span class="n">connect_args</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'argument1'</span><span class="p">:</span><span class="mi">17</span><span class="p">,</span> <span class="s1">'argument2'</span><span class="p">:</span><span class="s1">'bar'</span><span class="p">})</span></pre></div> </div> <p>The most customizable connection method of all is to pass a <code class="docutils literal notranslate"><span class="pre">creator</span></code> argument, which specifies a callable that returns a DBAPI connection:</p> <div class="highlight-python+sql notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">connect</span><span class="p">():</span> <span class="k">return</span> <span class="n">psycopg</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="s1">'scott'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s1">'localhost'</span><span class="p">)</span> <span class="n">db</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">'postgresql://'</span><span class="p">,</span> <span class="n">creator</span><span class="o">=</span><span class="n">connect</span><span class="p">)</span></pre></div> </div> </div> <div class="section" id="configuring-logging"> <span id="dbengine-logging"></span><h2>Configuring Logging<a class="headerlink" href="#configuring-logging" title="Permalink to this headline">¶</a></h2> <p>Python’s standard <a class="reference external" href="http://docs.python.org/library/logging.html">logging</a> 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 <a class="reference internal" href="#sqlalchemy.create_engine.params.echo" title="sqlalchemy.create_engine"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">create_engine.echo</span></code></a> and <a class="reference internal" href="#sqlalchemy.create_engine.params.echo_pool" title="sqlalchemy.create_engine"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">create_engine.echo_pool</span></code></a> present on <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> which allow immediate logging to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> for the purposes of local development; these parameters ultimately interact with the regular Python loggers described below.</p> <p>This section assumes familiarity with the above linked logging module. All logging performed by SQLAlchemy exists underneath the <code class="docutils literal notranslate"><span class="pre">sqlalchemy</span></code> namespace, as used by <code class="docutils literal notranslate"><span class="pre">logging.getLogger('sqlalchemy')</span></code>. When logging has been configured (i.e. such as via <code class="docutils literal notranslate"><span class="pre">logging.basicConfig()</span></code>), the general namespace of SA loggers that can be turned on is as follows:</p> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">sqlalchemy.engine</span></code> - controls SQL echoing. set to <code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> for SQL query output, <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code> for query + result set output. These settings are equivalent to <code class="docutils literal notranslate"><span class="pre">echo=True</span></code> and <code class="docutils literal notranslate"><span class="pre">echo="debug"</span></code> on <a class="reference internal" href="#sqlalchemy.create_engine.params.echo" title="sqlalchemy.create_engine"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">create_engine.echo</span></code></a>, respectively.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">sqlalchemy.pool</span></code> - controls connection pool logging. set to <code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> to log connection invalidation and recycle events; set to <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code> to additionally log all pool checkins and checkouts. These settings are equivalent to <code class="docutils literal notranslate"><span class="pre">pool_echo=True</span></code> and <code class="docutils literal notranslate"><span class="pre">pool_echo="debug"</span></code> on <a class="reference internal" href="#sqlalchemy.create_engine.params.echo_pool" title="sqlalchemy.create_engine"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">create_engine.echo_pool</span></code></a>, respectively.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">sqlalchemy.dialects</span></code> - controls custom logging for SQL dialects, to the extend that logging is used within specific dialects, which is generally minimal.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">sqlalchemy.orm</span></code> - controls logging of various ORM functions to the extent that logging is used within the ORM, which is generally minimal. Set to <code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> to log some top-level information on mapper configurations.</p></li> </ul> <p>For example, to log SQL queries using Python logging instead of the <code class="docutils literal notranslate"><span class="pre">echo=True</span></code> flag:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span> <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">()</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">'sqlalchemy.engine'</span><span class="p">)</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span></pre></div> </div> <p>By default, the log level is set to <code class="docutils literal notranslate"><span class="pre">logging.WARN</span></code> within the entire <code class="docutils literal notranslate"><span class="pre">sqlalchemy</span></code> namespace so that no log operations occur, even within an application that has logging enabled otherwise.</p> <p>The <code class="docutils literal notranslate"><span class="pre">echo</span></code> flags present as keyword arguments to <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a> and others as well as the <code class="docutils literal notranslate"><span class="pre">echo</span></code> property on <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>, when set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, will first attempt to ensure that logging is enabled. Unfortunately, the <code class="docutils literal notranslate"><span class="pre">logging</span></code> module provides no way of determining if output has already been configured (note we are referring to if a logging configuration has been set up, not just that the logging level is set). For this reason, any <code class="docutils literal notranslate"><span class="pre">echo=True</span></code> flags will result in a call to <code class="docutils literal notranslate"><span class="pre">logging.basicConfig()</span></code> using sys.stdout as the destination. It also sets up a default format using the level name, timestamp, and logger name. Note that this configuration has the affect of being configured <strong>in addition</strong> to any existing logger configurations. Therefore, <strong>when using Python logging, ensure all echo flags are set to False at all times</strong>, to avoid getting duplicate log lines.</p> <p>The logger name of instance such as an <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="pooling.html#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> defaults to using a truncated hex identifier string. To set this to a specific name, use the “logging_name” and “pool_logging_name” keyword arguments with <a class="reference internal" href="#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">sqlalchemy.create_engine()</span></code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The SQLAlchemy <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> conserves Python function call overhead by only emitting log statements when the current logging level is detected as <code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> or <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code>. 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 <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> that’s currently active, or more commonly a <a class="reference internal" href="../orm/session_api.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object that’s active in a transaction, won’t log any SQL according to the new configuration until a new <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> is procured (in the case of <a class="reference internal" href="../orm/session_api.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>, this is after the current transaction ends and a new one begins).</p> </div> </div> </div> </div> </div> <div id="docs-bottom-navigation" class="docs-navigation-links, withsidebar"> Previous: <a href="engines_connections.html" title="previous chapter">Engine and Connection Use</a> Next: <a href="connections.html" title="next chapter">Working with Engines and Connections</a> <div id="docs-copyright"> © <a href="../copyright.html">Copyright</a> 2007-2019, the SQLAlchemy authors and contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1. </div> </div> </div> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.2.19', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html' }; </script> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <!-- begin iterate through sphinx environment script_files --> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <!-- end iterate through sphinx environment script_files --> <script type="text/javascript" src="../_static/detectmobile.js"></script> <script type="text/javascript" src="../_static/init.js"></script> </body> </html>