asyncpg
asyncpg is a database interface library designed specifically for PostgreSQL and asyncio.
Aquesta pàgina encara no s'ha traduït — es mostra en l'idioma original:English
Introduction
asyncpg is an efficient implementation of the PostgreSQL binary protocol for Python’s asyncio. It provides fast queries, native protocol features, and convenient primitives for async I/O.
Setup
uv init asyncpg-basic
cd asyncpg-basic
uv add asyncpgCreate and start a PostgreSQL instance as explained in Basic.
Then create a database named test:
;Basic Usage
Create test_database.py:
# Establish a connection to an existing database named "test"
= await
# Execute a statement to create a new table.
await """
,
,
) )
# Insert a record into the created table.
await """
,
,
)
# Select a row from the table.
= await
# *row* now contains <Record id=1 name='Alice' dob=datetime.date(1980, 25, 1)>
# Close the connection.
await
Run the script and you should see:
<Record id=1 name='Alice' dob=datetime.date()Notes on parameters and DB-API
- asyncpg uses PostgreSQL’s native parameter placeholders:
$1,$2, … - asyncpg does not implement PEP 249 (DB-API 2.0). It favors PostgreSQL’s native parameterization and avoids query rewriting.