aiopg is a library for accessing a PostgreSQL database from the asyncio
Python Makefile
Latest commit c2e0e53 Feb 19, 2017 @jettify jettify committed on GitHub Merge pull request #276 from aio-libs/pyup-update-setuptools-34.1.1-t…

Update setuptools to 34.2.0



aiopg is a library for accessing a PostgreSQL database from the asyncio (PEP-3156/tulip) framework. It wraps asynchronous features of the Psycopg database driver.


import asyncio
import aiopg

dsn = 'dbname=aiopg user=aiopg password=passwd host='

async def go():
    pool = await aiopg.create_pool(dsn)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            ret = []
            async for row in cur:
            assert ret == [(1,)]

loop = asyncio.get_event_loop()

Example of SQLAlchemy optional integration

import asyncio
from import create_engine
import sqlalchemy as sa

metadata = sa.MetaData()

tbl = sa.Table('tbl', metadata,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('val', sa.String(255)))

async def create_table(engine):
    async with engine.acquire() as conn:
        await conn.execute('DROP TABLE IF EXISTS tbl')
        await conn.execute('''CREATE TABLE tbl (
                                  id serial PRIMARY KEY,
                                  val varchar(255))''')

async def go():
    async with create_engine(user='aiopg',
                             password='passwd') as engine:

        async with engine.acquire() as conn:
            await conn.execute(tbl.insert().values(val='abc'))

            async for row in conn.execute(
                print(, row.val)

loop = asyncio.get_event_loop()

For yield from based code, see the ./examples folder, files with old_style part in their names.

Please use:

$ make test

for executing the project's unittests. See CONTRIBUTING.rst for details on how to set up your environment to run the tests.