1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
.. _asyncpg-examples:
asyncpg Usage
=============
The interaction with the database normally starts with a call to
:func:`connect() <asyncpg.connection.connect>`, which establishes
a new database session and returns a new
:class:`Connection <asyncpg.connection.Connection>` instance,
which provides methods to run queries and manage transactions.
.. code-block:: python
import asyncio
import asyncpg
import datetime
async def main():
# Establish a connection to an existing database named "test"
# as a "postgres" user.
conn = await asyncpg.connect('postgresql://postgres@localhost/test')
# Execute a statement to create a new table.
await conn.execute('''
CREATE TABLE users(
id serial PRIMARY KEY,
name text,
dob date
)
''')
# Insert a record into the created table.
await conn.execute('''
INSERT INTO users(name, dob) VALUES($1, $2)
''', 'Bob', datetime.date(1984, 3, 1))
# Select a row from the table.
row = await conn.fetchrow(
'SELECT * FROM users WHERE name = $1', 'Bob')
# *row* now contains
# asyncpg.Record(id=1, name='Bob', dob=datetime.date(1984, 3, 1))
# Close the connection.
await conn.close()
asyncio.run(main())
.. note::
asyncpg uses the native PostgreSQL syntax for query arguments: ``$n``.
Type Conversion
---------------
asyncpg automatically converts PostgreSQL types to the corresponding Python
types and vice versa. All standard data types are supported out of the box,
including arrays, composite types, range types, enumerations and any
combination of them. It is possible to supply codecs for non-standard
types or override standard codecs. See :ref:`asyncpg-custom-codecs` for
more information.
The table below shows the correspondence between PostgreSQL and Python types.
+----------------------+-----------------------------------------------------+
| PostgreSQL Type | Python Type |
+======================+=====================================================+
| ``anyarray`` | :class:`list <python:list>` |
+----------------------+-----------------------------------------------------+
| ``anyenum`` | :class:`str <python:str>` |
+----------------------+-----------------------------------------------------+
| ``anyrange`` | :class:`asyncpg.Range <asyncpg.types.Range>`, |
| | :class:`tuple <python:tuple>` |
+----------------------+-----------------------------------------------------+
| ``anymultirange`` | ``list[``:class:`asyncpg.Range\ |
| | <asyncpg.types.Range>` ``]``, |
| | ``list[``:class:`tuple <python:tuple>` ``]`` [#f1]_ |
+----------------------+-----------------------------------------------------+
| ``record`` | :class:`asyncpg.Record`, |
| | :class:`tuple <python:tuple>`, |
| | :class:`Mapping <python:collections.abc.Mapping>` |
+----------------------+-----------------------------------------------------+
| ``bit``, ``varbit`` | :class:`asyncpg.BitString <asyncpg.types.BitString>`|
+----------------------+-----------------------------------------------------+
| ``bool`` | :class:`bool <python:bool>` |
+----------------------+-----------------------------------------------------+
| ``box`` | :class:`asyncpg.Box <asyncpg.types.Box>` |
+----------------------+-----------------------------------------------------+
| ``bytea`` | :class:`bytes <python:bytes>` |
+----------------------+-----------------------------------------------------+
| ``char``, ``name``, | :class:`str <python:str>` |
| ``varchar``, | |
| ``text``, | |
| ``xml`` | |
+----------------------+-----------------------------------------------------+
| ``cidr`` | :class:`ipaddress.IPv4Network\ |
| | <python:ipaddress.IPv4Network>`, |
| | :class:`ipaddress.IPv6Network\ |
| | <python:ipaddress.IPv6Network>` |
+----------------------+-----------------------------------------------------+
| ``inet`` | :class:`ipaddress.IPv4Interface\ |
| | <python:ipaddress.IPv4Interface>`, |
| | :class:`ipaddress.IPv6Interface\ |
| | <python:ipaddress.IPv6Interface>`, |
| | :class:`ipaddress.IPv4Address\ |
| | <python:ipaddress.IPv4Address>`, |
| | :class:`ipaddress.IPv6Address\ |
| | <python:ipaddress.IPv6Address>` [#f2]_ |
+----------------------+-----------------------------------------------------+
| ``macaddr`` | :class:`str <python:str>` |
+----------------------+-----------------------------------------------------+
| ``circle`` | :class:`asyncpg.Circle <asyncpg.types.Circle>` |
+----------------------+-----------------------------------------------------+
| ``date`` | :class:`datetime.date <python:datetime.date>` |
+----------------------+-----------------------------------------------------+
| ``time`` | offset-naïve :class:`datetime.time \ |
| | <python:datetime.time>` |
+----------------------+-----------------------------------------------------+
| ``time with | offset-aware :class:`datetime.time \ |
| time zone`` | <python:datetime.time>` |
+----------------------+-----------------------------------------------------+
| ``timestamp`` | offset-naïve :class:`datetime.datetime \ |
| | <python:datetime.datetime>` |
+----------------------+-----------------------------------------------------+
| ``timestamp with | offset-aware :class:`datetime.datetime \ |
| time zone`` | <python:datetime.datetime>` |
+----------------------+-----------------------------------------------------+
| ``interval`` | :class:`datetime.timedelta \ |
| | <python:datetime.timedelta>` |
+----------------------+-----------------------------------------------------+
| ``float``, | :class:`float <python:float>` [#f3]_ |
| ``double precision`` | |
+----------------------+-----------------------------------------------------+
| ``smallint``, | :class:`int <python:int>` |
| ``integer``, | |
| ``bigint`` | |
+----------------------+-----------------------------------------------------+
| ``numeric`` | :class:`Decimal <python:decimal.Decimal>` |
+----------------------+-----------------------------------------------------+
| ``json``, ``jsonb`` | :class:`str <python:str>` |
+----------------------+-----------------------------------------------------+
| ``line`` | :class:`asyncpg.Line <asyncpg.types.Line>` |
+----------------------+-----------------------------------------------------+
| ``lseg`` | :class:`asyncpg.LineSegment \ |
| | <asyncpg.types.LineSegment>` |
+----------------------+-----------------------------------------------------+
| ``money`` | :class:`str <python:str>` |
+----------------------+-----------------------------------------------------+
| ``path`` | :class:`asyncpg.Path <asyncpg.types.Path>` |
+----------------------+-----------------------------------------------------+
| ``point`` | :class:`asyncpg.Point <asyncpg.types.Point>` |
+----------------------+-----------------------------------------------------+
| ``polygon`` | :class:`asyncpg.Polygon <asyncpg.types.Polygon>` |
+----------------------+-----------------------------------------------------+
| ``uuid`` | :class:`uuid.UUID <python:uuid.UUID>` |
+----------------------+-----------------------------------------------------+
| ``tid`` | :class:`tuple <python:tuple>` |
+----------------------+-----------------------------------------------------+
All other types are encoded and decoded as text by default.
.. [#f1] Since version 0.25.0
.. [#f2] Prior to version 0.20.0, asyncpg erroneously treated ``inet`` values
with prefix as ``IPvXNetwork`` instead of ``IPvXInterface``.
.. [#f3] Inexact single-precision ``float`` values may have a different
representation when decoded into a Python float. This is inherent
to the implementation of limited-precision floating point types.
If you need the decimal representation to match, cast the expression
to ``double`` or ``numeric`` in your query.
.. _asyncpg-custom-codecs:
Custom Type Conversions
-----------------------
asyncpg allows defining custom type conversion functions both for standard
and user-defined types using the :meth:`Connection.set_type_codec() \
<asyncpg.connection.Connection.set_type_codec>` and
:meth:`Connection.set_builtin_type_codec() \
<asyncpg.connection.Connection.set_builtin_type_codec>` methods.
Example: automatic JSON conversion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The example below shows how to configure asyncpg to encode and decode
JSON values using the :mod:`json <python:json>` module.
.. code-block:: python
import asyncio
import asyncpg
import json
async def main():
conn = await asyncpg.connect()
try:
await conn.set_type_codec(
'json',
encoder=json.dumps,
decoder=json.loads,
schema='pg_catalog'
)
data = {'foo': 'bar', 'spam': 1}
res = await conn.fetchval('SELECT $1::json', data)
finally:
await conn.close()
asyncio.run(main())
Example: complex types
~~~~~~~~~~~~~~~~~~~~~~
The example below shows how to configure asyncpg to encode and decode
Python :class:`complex <python:complex>` values to a custom composite
type in PostgreSQL.
.. code-block:: python
import asyncio
import asyncpg
async def main():
conn = await asyncpg.connect()
try:
await conn.execute(
'''
CREATE TYPE mycomplex AS (
r float,
i float
);'''
)
await conn.set_type_codec(
'complex',
encoder=lambda x: (x.real, x.imag),
decoder=lambda t: complex(t[0], t[1]),
format='tuple',
)
res = await conn.fetchval('SELECT $1::mycomplex', (1+2j))
finally:
await conn.close()
asyncio.run(main())
Example: automatic conversion of PostGIS types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The example below shows how to configure asyncpg to encode and decode
the PostGIS ``geometry`` type. It works for any Python object that
conforms to the `geo interface specification`_ and relies on Shapely_,
although any library that supports reading and writing the WKB format
will work.
.. _Shapely: https://github.com/Toblerity/Shapely
.. _geo interface specification: https://gist.github.com/sgillies/2217756
.. code-block:: python
import asyncio
import asyncpg
import shapely.geometry
import shapely.wkb
from shapely.geometry.base import BaseGeometry
async def main():
conn = await asyncpg.connect()
try:
def encode_geometry(geometry):
if not hasattr(geometry, '__geo_interface__'):
raise TypeError('{g} does not conform to '
'the geo interface'.format(g=geometry))
shape = shapely.geometry.shape(geometry)
return shapely.wkb.dumps(shape)
def decode_geometry(wkb):
return shapely.wkb.loads(wkb)
await conn.set_type_codec(
'geometry', # also works for 'geography'
encoder=encode_geometry,
decoder=decode_geometry,
format='binary',
)
data = shapely.geometry.Point(-73.985661, 40.748447)
res = await conn.fetchrow(
'''SELECT 'Empire State Building' AS name,
$1::geometry AS coordinates
''',
data)
print(res)
finally:
await conn.close()
asyncio.run(main())
Example: decoding numeric columns as floats
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
By default asyncpg decodes numeric columns as Python
:class:`Decimal <python:decimal.Decimal>` instances. The example below
shows how to instruct asyncpg to use floats instead.
.. code-block:: python
import asyncio
import asyncpg
async def main():
conn = await asyncpg.connect()
try:
await conn.set_type_codec(
'numeric', encoder=str, decoder=float,
schema='pg_catalog', format='text'
)
res = await conn.fetchval("SELECT $1::numeric", 11.123)
print(res, type(res))
finally:
await conn.close()
asyncio.run(main())
Example: decoding hstore values
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hstore_ is an extension data type used for storing key/value pairs.
asyncpg includes a codec to decode and encode hstore values as ``dict``
objects. Because ``hstore`` is not a builtin type, the codec must
be registered on a connection using :meth:`Connection.set_builtin_type_codec()
<asyncpg.connection.Connection.set_builtin_type_codec>`:
.. code-block:: python
import asyncpg
import asyncio
async def run():
conn = await asyncpg.connect()
# Assuming the hstore extension exists in the public schema.
await conn.set_builtin_type_codec(
'hstore', codec_name='pg_contrib.hstore')
result = await conn.fetchval("SELECT 'a=>1,b=>2,c=>NULL'::hstore")
assert result == {'a': '1', 'b': '2', 'c': None}
asyncio.run(run())
.. _hstore: https://www.postgresql.org/docs/current/static/hstore.html
Transactions
------------
To create transactions, the
:meth:`Connection.transaction() <asyncpg.connection.Connection>` method
should be used.
The most common way to use transactions is through an ``async with`` statement:
.. code-block:: python
async with connection.transaction():
await connection.execute("INSERT INTO mytable VALUES(1, 2, 3)")
.. note::
When not in an explicit transaction block, any changes to the database
will be applied immediately. This is also known as *auto-commit*.
See the :ref:`asyncpg-api-transaction` API documentation for more information.
.. _asyncpg-connection-pool:
Connection Pools
----------------
For server-type type applications, that handle frequent requests and need
the database connection for a short period time while handling a request,
the use of a connection pool is recommended. asyncpg provides an advanced
pool implementation, which eliminates the need to use an external connection
pooler such as PgBouncer.
To create a connection pool, use the
:func:`asyncpg.create_pool() <asyncpg.pool.create_pool>` function.
The resulting :class:`Pool <asyncpg.pool.Pool>` object can then be used
to borrow connections from the pool.
Below is an example of how **asyncpg** can be used to implement a simple
Web service that computes the requested power of two.
.. code-block:: python
import asyncio
import asyncpg
from aiohttp import web
async def handle(request):
"""Handle incoming requests."""
pool = request.app['pool']
power = int(request.match_info.get('power', 10))
# Take a connection from the pool.
async with pool.acquire() as connection:
# Open a transaction.
async with connection.transaction():
# Run the query passing the request argument.
result = await connection.fetchval('select 2 ^ $1', power)
return web.Response(
text="2 ^ {} is {}".format(power, result))
async def init_db(app):
"""Initialize a connection pool."""
app['pool'] = await asyncpg.create_pool(database='postgres',
user='postgres')
yield
await app['pool'].close()
def init_app():
"""Initialize the application server."""
app = web.Application()
# Create a database context
app.cleanup_ctx.append(init_db)
# Configure service routes
app.router.add_route('GET', '/{power:\d+}', handle)
app.router.add_route('GET', '/', handle)
return app
app = init_app()
web.run_app(app)
See :ref:`asyncpg-api-pool` API documentation for more information.
|