db = create_engine('postgresql://scott:tiger@localhost/test', connect_args = {'argument1':17, 'argument2':'bar'})
-The most customizable connection method of all is to pass a ``creator``
-argument, which specifies a callable that returns a DBAPI connection:
+The two methods that are the most customizable include using the
+:paramref:`_sa.create_engine.creator` parameter, which specifies a callable that returns a
+DBAPI connection:
.. sourcecode:: python+sql
db = create_engine('postgresql://', creator=connect)
+Alternatively, the :meth:`_events.DialectEvents.do_connect` hook may be
+used on an existing engine which allows full replacement of the connection
+approach, given connection arguments::
+
+
+ from sqlalchemy import event
+
+ db = create_engine('postgresql://scott:tiger@localhost/test')
+
+ @event.listens_for(db, "do_connect")
+ def receive_do_connect(dialect, conn_rec, cargs, cparams):
+ # cargs and cparams can be modified in place...
+ cparams['password'] = 'new password'
+
+ # alternatively, return the new DBAPI connection
+ return psycopg2.connect(*cargs, **cparams)
.. _dbengine_logging:
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
+ :class:`_events.DialectEvents.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.
+
+ .. seealso::
+
+ :class:`_events.DialectEvents.do_connect` - event hook that allows
+ full control over DBAPI connection mechanics.
+
+ :ref:`custom_dbapi_args`
+
:param 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