Top Related Projects
A fast PostgreSQL Database Client Library for Python/asyncio.
Async database support for Python. 🗄
Familiar asyncio ORM for python, built with relations in mind
A fast, user friendly ORM and query builder which supports asyncio.
An async ORM. 🗃
Quick Overview
aiopg is an asynchronous PostgreSQL driver for Python, built on top of psycopg2 and asyncio. It provides a convenient way to work with PostgreSQL databases in asynchronous Python applications, allowing for efficient handling of multiple database connections and queries concurrently.
Pros
- Asynchronous operations for improved performance in I/O-bound applications
- Compatible with asyncio, making it easy to integrate with other async libraries
- Supports both connection pooling and individual connections
- Built on top of psycopg2, leveraging its stability and features
Cons
- Requires understanding of asynchronous programming concepts
- Limited to PostgreSQL databases only
- May have a steeper learning curve compared to synchronous alternatives
- Not suitable for CPU-bound database operations
Code Examples
- Establishing a connection and executing a query:
import asyncio
import aiopg
async def fetch_data():
dsn = 'dbname=test user=postgres password=secret'
async with aiopg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM users")
return await cur.fetchall()
results = asyncio.run(fetch_data())
print(results)
- Using a connection pool:
import aiopg
async def init_pool():
dsn = 'dbname=test user=postgres password=secret'
return await aiopg.create_pool(dsn)
async def fetch_user(pool, user_id):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
return await cur.fetchone()
# Usage
pool = await init_pool()
user = await fetch_user(pool, 1)
- Executing a transaction:
async def transfer_funds(pool, from_account, to_account, amount):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await conn.begin()
try:
await cur.execute("UPDATE accounts SET balance = balance - %s WHERE id = %s", (amount, from_account))
await cur.execute("UPDATE accounts SET balance = balance + %s WHERE id = %s", (amount, to_account))
except Exception:
await conn.rollback()
raise
else:
await conn.commit()
Getting Started
To get started with aiopg, first install it using pip:
pip install aiopg
Then, you can use it in your async Python code:
import asyncio
import aiopg
async def main():
dsn = 'dbname=yourdb user=youruser password=yourpassword host=localhost'
pool = await aiopg.create_pool(dsn)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
ret = await cur.fetchone()
print(ret)
asyncio.run(main())
This example establishes a connection pool, acquires a connection, executes a simple query, and prints the result.
Competitor Comparisons
A fast PostgreSQL Database Client Library for Python/asyncio.
Pros of asyncpg
- Higher performance due to direct implementation of PostgreSQL protocol
- Native support for PostgreSQL-specific features like JSONB and arrays
- More actively maintained with frequent updates and improvements
Cons of asyncpg
- Limited to PostgreSQL, while aiopg supports multiple databases through SQLAlchemy
- Steeper learning curve due to its low-level API compared to aiopg's familiar SQLAlchemy interface
- Lacks some higher-level ORM features that aiopg provides through SQLAlchemy
Code Comparison
asyncpg example:
async with asyncpg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
result = await conn.fetch('SELECT * FROM users')
aiopg example:
async with aiopg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute('SELECT * FROM users')
result = await cur.fetchall()
The asyncpg example is more concise, reflecting its lower-level nature and direct PostgreSQL protocol implementation. The aiopg example uses a familiar cursor-based approach, similar to synchronous Python database APIs.
Both libraries offer async/await syntax and connection pooling, but asyncpg's direct protocol implementation gives it a performance edge for PostgreSQL-specific use cases. However, aiopg's SQLAlchemy integration provides more flexibility and higher-level abstractions, which may be preferable for complex applications or those requiring database agnosticism.
Async database support for Python. 🗄
Pros of databases
- Supports multiple database backends (PostgreSQL, MySQL, SQLite)
- Provides a higher-level, more intuitive API
- Includes built-in connection pooling
Cons of databases
- Less mature and potentially less stable than aiopg
- May have fewer advanced PostgreSQL-specific features
- Slightly higher learning curve for developers familiar with SQLAlchemy
Code Comparison
databases:
from databases import Database
database = Database("postgresql://user:pass@localhost/dbname")
async def main():
await database.connect()
query = "SELECT * FROM users WHERE id = :id"
result = await database.fetch_one(query=query, values={"id": 1})
aiopg:
import aiopg
async def main():
async with aiopg.create_pool("dbname=test user=postgres") as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM users WHERE id = %s", (1,))
result = await cur.fetchone()
The databases library offers a more concise and higher-level API, while aiopg provides a lower-level interface that closely mirrors the standard psycopg2 library. databases abstracts away connection management and cursor creation, making it easier to write and maintain database-related code.
Familiar asyncio ORM for python, built with relations in mind
Pros of Tortoise-ORM
- Provides a full-featured ORM with model definitions, relationships, and query building
- Supports multiple databases (PostgreSQL, MySQL, SQLite) with a unified API
- Includes built-in migration support for easier schema management
Cons of Tortoise-ORM
- Higher learning curve due to more complex API and ORM concepts
- Potentially slower performance for simple queries compared to raw SQL
- Less flexibility for fine-tuning database-specific optimizations
Code Comparison
Tortoise-ORM:
from tortoise import fields, models
class User(models.Model):
name = fields.CharField(max_length=50)
age = fields.IntField()
await User.create(name="John", age=30)
users = await User.filter(age__gte=18).order_by("name")
aiopg:
async with aiopg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ("John", 30))
await cur.execute("SELECT * FROM users WHERE age >= 18 ORDER BY name")
users = await cur.fetchall()
Tortoise-ORM offers a more Pythonic and ORM-style approach, while aiopg provides lower-level database access with raw SQL queries. The choice between them depends on the project's requirements, complexity, and developer preferences.
A fast, user friendly ORM and query builder which supports asyncio.
Pros of Piccolo
- Full-featured ORM with support for schema migrations, query building, and admin interface
- Built-in async support, leveraging Python's asyncio capabilities
- Includes a CLI tool for database management and schema migrations
Cons of Piccolo
- Steeper learning curve due to its comprehensive feature set
- Less mature and less widely adopted compared to aiopg
- May be overkill for simple database operations or small projects
Code Comparison
Piccolo example:
from piccolo.tables import Table
from piccolo.columns import Varchar, Integer
class User(Table):
name = Varchar()
age = Integer()
await User.objects().create(name="John", age=30)
aiopg example:
import aiopg
async with aiopg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ("John", 30))
Piccolo provides a more high-level, ORM-style approach, while aiopg offers lower-level database access. Piccolo abstracts away SQL queries, making it easier to work with database objects, but potentially sacrificing fine-grained control. aiopg, on the other hand, requires more manual SQL writing but offers greater flexibility for complex queries and optimizations.
An async ORM. 🗃
Pros of orm
- More comprehensive ORM functionality with support for model definitions and relationships
- Built-in query builder for constructing complex queries
- Supports multiple database backends, including PostgreSQL, MySQL, and SQLite
Cons of orm
- Steeper learning curve due to more complex API and features
- Potentially slower performance for simple queries compared to raw SQL
Code Comparison
orm:
class User(orm.Model):
id = orm.Integer(primary_key=True)
name = orm.String(max_length=100)
users = await User.objects.filter(name__icontains="john").all()
aiopg:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM users WHERE name ILIKE %s", ('%john%',))
users = await cur.fetchall()
Summary
orm offers a more feature-rich ORM experience with support for multiple databases, while aiopg provides a simpler, PostgreSQL-specific async driver. orm's higher-level abstractions come at the cost of a steeper learning curve and potential performance overhead for simple queries. aiopg offers more direct control over SQL queries but requires more manual query writing and result processing.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
aiopg
.. image:: https://github.com/aio-libs/aiopg/workflows/CI/badge.svg :target: https://github.com/aio-libs/aiopg/actions?query=workflow%3ACI .. image:: https://codecov.io/gh/aio-libs/aiopg/branch/master/graph/badge.svg :target: https://codecov.io/gh/aio-libs/aiopg .. image:: https://badges.gitter.im/Join%20Chat.svg :target: https://gitter.im/aio-libs/Lobby :alt: 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
.. code:: python
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
.. code:: python
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())
.. _PostgreSQL: http://www.postgresql.org/ .. _asyncio: https://docs.python.org/3/library/asyncio.html
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.
Top Related Projects
A fast PostgreSQL Database Client Library for Python/asyncio.
Async database support for Python. 🗄
Familiar asyncio ORM for python, built with relations in mind
A fast, user friendly ORM and query builder which supports asyncio.
An async ORM. 🗃
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot