Skip to content
/ aiopg Public

aiopg is a library for accessing a PostgreSQL database from the asyncio

License

Notifications You must be signed in to change notification settings

aio-libs/aiopg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

7b01833 · Aug 30, 2023
Aug 30, 2023
Aug 30, 2023
Aug 30, 2023
Aug 25, 2023
Aug 30, 2023
Dec 19, 2015
Mar 22, 2021
Sep 23, 2019
May 1, 2018
Sep 23, 2019
Aug 29, 2023
Sep 23, 2019
Sep 23, 2019
Dec 4, 2020
Dec 7, 2020
Dec 4, 2020
Apr 3, 2021
Jul 5, 2021
Aug 25, 2023
Mar 31, 2017
Aug 30, 2023

aiopg

Chat on Gitter

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.

Example

import asyncio
import aiopg

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

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:
                ret.append(row)
            assert ret == [(1,)]

loop = asyncio.get_event_loop()
loop.run_until_complete(go())

Example of SQLAlchemy optional integration

import asyncio
from aiopg.sa 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',
                             database='aiopg',
                             host='127.0.0.1',
                             password='passwd') as engine:

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

            async for row in conn.execute(tbl.select()):
                print(row.id, row.val)

loop = asyncio.get_event_loop()
loop.run_until_complete(go())

Please use:

$ make test

for executing the project's unittests. See https://aiopg.readthedocs.io/en/stable/contributing.html for details on how to set up your environment to run the tests.