Top Related Projects
aiopg is a library for accessing a PostgreSQL database from the asyncio
Async database support for Python. 🗄
Familiar asyncio ORM for python, built with relations in mind
The Database Toolkit for Python
New generation PostgreSQL database adapter for the Python programming language
Quick Overview
AsyncPG is a high-performance PostgreSQL database client library for Python, designed specifically for asyncio-based applications. It provides an efficient, fully asynchronous interface for interacting with PostgreSQL databases, making it ideal for building high-concurrency applications.
Pros
- Extremely fast performance, often outperforming other PostgreSQL clients
- Full support for asyncio, allowing for non-blocking database operations
- Type-aware, with automatic conversion between PostgreSQL and Python data types
- Supports advanced PostgreSQL features like LISTEN/NOTIFY and COPY
Cons
- Requires Python 3.6+ and only works with asyncio-based applications
- Steeper learning curve compared to synchronous database libraries
- Limited support for ORMs (Object-Relational Mapping) tools
- May require more complex error handling due to its asynchronous nature
Code Examples
- Connecting to a database and executing a query:
import asyncio
import asyncpg
async def run():
conn = await asyncpg.connect(user='user', password='password',
database='database', host='localhost')
values = await conn.fetch('SELECT * FROM users WHERE active = true')
await conn.close()
asyncio.run(run())
- Using a connection pool:
import asyncpg
async def get_user(pool, user_id):
async with pool.acquire() as conn:
return await conn.fetchrow('SELECT * FROM users WHERE id = $1', user_id)
# Usage
pool = await asyncpg.create_pool(user='user', password='password',
database='database', host='localhost')
user = await get_user(pool, 123)
- Executing a transaction:
async def transfer_funds(conn, from_account, to_account, amount):
async with conn.transaction():
await conn.execute('UPDATE accounts SET balance = balance - $1 '
'WHERE account_id = $2', amount, from_account)
await conn.execute('UPDATE accounts SET balance = balance + $1 '
'WHERE account_id = $2', amount, to_account)
Getting Started
To get started with AsyncPG, first install it using pip:
pip install asyncpg
Then, you can use it in your asyncio-based Python application:
import asyncio
import asyncpg
async def main():
conn = await asyncpg.connect(user='user', password='password',
database='database', host='localhost')
# Create a table
await conn.execute('''
CREATE TABLE users(
id serial PRIMARY KEY,
name text,
dob date
)
''')
# Insert a record
await conn.execute('''
INSERT INTO users(name, dob) VALUES($1, $2)
''', 'John Doe', '1990-01-01')
# Query the table
row = await conn.fetchrow('SELECT * FROM users WHERE name = $1', 'John Doe')
print(row)
await conn.close()
asyncio.run(main())
This example demonstrates connecting to a database, creating a table, inserting data, and querying the table using AsyncPG.
Competitor Comparisons
aiopg is a library for accessing a PostgreSQL database from the asyncio
Pros of aiopg
- Built on top of psycopg2, providing familiar API and features
- Supports both high-level SQLAlchemy integration and low-level cursor operations
- More mature project with longer development history
Cons of aiopg
- Generally slower performance compared to asyncpg
- Lacks some advanced PostgreSQL-specific features
- Requires separate installation of psycopg2
Code Comparison
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()
asyncpg example:
async with asyncpg.create_pool(dsn) as pool:
async with pool.acquire() as conn:
result = await conn.fetch("SELECT * FROM users")
Both libraries provide asynchronous connection pools and execute SQL queries. However, asyncpg offers a more streamlined API, eliminating the need for an explicit cursor object. This results in slightly cleaner code and potentially better performance.
While aiopg builds upon the widely-used psycopg2 driver, asyncpg is a complete reimplementation focused on asyncio and PostgreSQL-specific features. This allows asyncpg to achieve better performance but at the cost of some compatibility with existing psycopg2-based code.
Choose aiopg if you need SQLAlchemy integration or have existing psycopg2 code. Opt for asyncpg if you prioritize performance and PostgreSQL-specific features in a pure asyncio environment.
Async database support for Python. 🗄
Pros of databases
- Supports multiple database backends (PostgreSQL, MySQL, SQLite)
- Provides a high-level, database-agnostic API
- Integrates well with other Encode projects like FastAPI and Starlette
Cons of databases
- Generally slower performance compared to asyncpg
- Less feature-rich for PostgreSQL-specific functionality
- Higher-level abstraction may limit fine-grained control
Code Comparison
databases:
import databases
database = databases.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})
asyncpg:
import asyncpg
async def main():
conn = await asyncpg.connect("postgresql://user:pass@localhost/dbname")
result = await conn.fetchrow("SELECT * FROM users WHERE id = $1", 1)
Summary
databases offers a more versatile solution for projects requiring support for multiple database backends, while asyncpg provides superior performance and more advanced PostgreSQL-specific features. The choice between the two depends on project requirements, with databases being more suitable for multi-database applications and asyncpg excelling in PostgreSQL-focused environments.
Familiar asyncio ORM for python, built with relations in mind
Pros of Tortoise-ORM
- Provides a high-level ORM with support for multiple databases
- Offers a more Pythonic and intuitive API for database operations
- Includes built-in support for database migrations
Cons of Tortoise-ORM
- Generally slower performance compared to asyncpg
- Less fine-grained control over SQL queries and database operations
- Steeper learning curve for developers new to ORMs
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)
asyncpg:
import asyncpg
conn = await asyncpg.connect(...)
await conn.execute('''
INSERT INTO users(name, age) VALUES($1, $2)
''', 'John', 30)
Tortoise-ORM provides a more abstract and Pythonic approach to database operations, while asyncpg offers lower-level control and potentially better performance for complex queries. Tortoise-ORM is better suited for projects requiring a full-featured ORM with migration support, while asyncpg is ideal for applications needing fine-grained control over database operations and maximum performance.
The Database Toolkit for Python
Pros of SQLAlchemy
- Supports multiple database backends, not limited to PostgreSQL
- Provides a high-level ORM for database interactions
- Extensive documentation and large community support
Cons of SQLAlchemy
- Generally slower performance compared to asyncpg
- More complex setup and steeper learning curve
- Lacks native async support (though there's an extension for it)
Code Comparison
SQLAlchemy:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
asyncpg:
import asyncpg
async def create_user(conn, user_id, name):
await conn.execute('''
INSERT INTO users(id, name) VALUES($1, $2)
''', user_id, name)
SQLAlchemy provides a more abstract, ORM-based approach, while asyncpg offers a more direct, SQL-focused method. SQLAlchemy's ORM can simplify complex queries and relationships, but asyncpg's approach can lead to better performance for simpler operations. The choice between them often depends on the specific needs of the project, such as database compatibility, performance requirements, and developer preferences.
New generation PostgreSQL database adapter for the Python programming language
Pros of psycopg
- Mature and widely adopted PostgreSQL adapter for Python
- Supports both synchronous and asynchronous operations
- Extensive documentation and community support
Cons of psycopg
- Generally slower performance compared to asyncpg
- More complex API for asynchronous operations
- Larger memory footprint
Code Comparison
psycopg (synchronous):
import psycopg
with psycopg.connect("dbname=test user=postgres") as conn:
with conn.cursor() as cur:
cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
result = cur.fetchone()
asyncpg (asynchronous):
import asyncpg
async with asyncpg.connect('postgresql://user@host/database') as conn:
result = await conn.fetchrow('SELECT * FROM users WHERE id = $1', user_id)
asyncpg offers a more streamlined API for asynchronous operations, while psycopg provides a familiar interface for developers accustomed to synchronous database interactions. asyncpg generally outperforms psycopg in terms of speed and efficiency, especially for high-concurrency scenarios. However, psycopg's maturity, extensive feature set, and support for both synchronous and asynchronous operations make it a versatile choice for many projects.
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
asyncpg -- A fast PostgreSQL Database Client Library for Python/asyncio
.. image:: https://github.com/MagicStack/asyncpg/workflows/Tests/badge.svg :target: https://github.com/MagicStack/asyncpg/actions?query=workflow%3ATests+branch%3Amaster :alt: GitHub Actions status .. image:: https://img.shields.io/pypi/v/asyncpg.svg :target: https://pypi.python.org/pypi/asyncpg
asyncpg is a database interface library designed specifically for
PostgreSQL and Python/asyncio. asyncpg is an efficient, clean implementation
of PostgreSQL server binary protocol for use with Python's asyncio
framework. You can read more about asyncpg in an introductory
blog post <http://magic.io/blog/asyncpg-1m-rows-from-postgres-to-python/>
_.
asyncpg requires Python 3.8 or later and is supported for PostgreSQL versions 9.5 to 17. Other PostgreSQL versions or other databases implementing the PostgreSQL protocol may work, but are not being actively tested.
Documentation
The project documentation can be found
here <https://magicstack.github.io/asyncpg/current/>
_.
Performance
In our testing asyncpg is, on average, 5x faster than psycopg3.
.. image:: https://raw.githubusercontent.com/MagicStack/asyncpg/master/performance.png?fddca40ab0 :target: https://gistpreview.github.io/?0ed296e93523831ea0918d42dd1258c2
The above results are a geometric mean of benchmarks obtained with PostgreSQL
client driver benchmarking toolbench <https://github.com/MagicStack/pgbench>
_
in June 2023 (click on the chart to see full details).
Features
asyncpg implements PostgreSQL server protocol natively and exposes its features directly, as opposed to hiding them behind a generic facade like DB-API.
This enables asyncpg to have easy-to-use support for:
- prepared statements
- scrollable cursors
- partial iteration on query results
- automatic encoding and decoding of composite types, arrays, and any combination of those
- straightforward support for custom data types
Installation
asyncpg is available on PyPI. When not using GSSAPI/SSPI authentication it has no dependencies. Use pip to install::
$ pip install asyncpg
If you need GSSAPI/SSPI authentication, use::
$ pip install 'asyncpg[gssauth]'
For more details, please see the documentation <https://magicstack.github.io/asyncpg/current/installation.html>
_.
Basic Usage
.. code-block:: python
import asyncio
import asyncpg
async def run():
conn = await asyncpg.connect(user='user', password='password',
database='database', host='127.0.0.1')
values = await conn.fetch(
'SELECT * FROM mytable WHERE id = $1',
10,
)
await conn.close()
asyncio.run(run())
License
asyncpg is developed and distributed under the Apache 2.0 license.
Top Related Projects
aiopg is a library for accessing a PostgreSQL database from the asyncio
Async database support for Python. 🗄
Familiar asyncio ORM for python, built with relations in mind
The Database Toolkit for Python
New generation PostgreSQL database adapter for the Python programming language
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