psycopg2 VS asyncpg

Compare psycopg2 vs asyncpg and see what are their differences.

psycopg2

PostgreSQL database adapter for the Python programming language (by psycopg)

asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio. (by MagicStack)
Our great sponsors
  • Scout APM - Truly a developer’s best friend
  • InfluxDB - Build time-series-based applications quickly and at scale.
  • SonarQube - Static code analysis for 29 languages.
  • Zigi - Workflow assistant built for devs & their teams
psycopg2 asyncpg
14 9
2,815 5,739
1.2% 0.9%
5.4 5.3
29 days ago 4 days ago
C Python
GNU General Public License v3.0 or later Apache License 2.0
The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives.
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.

psycopg2

Posts with mentions or reviews of psycopg2. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2022-10-11.

asyncpg

Posts with mentions or reviews of asyncpg. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2022-06-28.
  • aiopg vs asyncpg vs psycopg3
    3 projects | reddit.com/r/learnpython | 28 Jun 2022
    asyncpg: 5.5k starts, last commit recently, ~150 issues, some incompatibility, few open PRs, extensive README. Includes benchmark showing it's supposedly 3x faster than aiopg and psycopg2, psycopg3 is not mentioned in the benchmark.
  • Announcing Quart-DB
    3 projects | reddit.com/r/Python | 12 Apr 2022
    Quart-DB uses asyncpg to manage the connections and buildpg to parse the named parameter bindings.
  • Cascade of doom: JIT, and how a Postgres update led to 70% failure on a critical national service
    7 projects | dev.to | 13 Nov 2021
    Simple query runs long when DB schema contains thousands of tables #186
  • Large database to python - SQL advantageous? (mysql)
    2 projects | reddit.com/r/Python | 4 Oct 2021
    Highly recommend https://github.com/MagicStack/asyncpg.
  • Postgrey - Simple, Fast & Async Library for PostgreSQL
    2 projects | dev.to | 29 Jun 2021
    🐘 Simple, Fast, Async & ORM PostgreSQL database client based on Asyncpg for Python.
  • New major versions of Flask, Jinja, Click, and Werkzeug released!
    9 projects | reddit.com/r/Python | 11 May 2021
    What we get with the async part of this release - If you would like to run an async library or your own async code from a flask route you can do that now. This is super useful, where let's say we have some async code that fetches data from many sources concurrently, or call multiple a few ML prediction endpoints at the same time (as long as they don't time out) using httpx and respond with some sort of outcome, or finally try that cool new async-only database library. A current (v2) limitation is that the you can't make concurrent requests using just the current asyncio implementation (an alternative with Flask API and ASGI: Quart). Typically in production gunicorn or uwsgi + threads/processes/gevent-eventlet is used and this makes Flask behave asynchronously. More here and here if interested.
  • Migrating to SQLAlchemy 2.0
    3 projects | news.ycombinator.com | 18 Feb 2021
    Agreed. I will never recommend an ORM, things simply spiraled out of control for medium to large-ish projects that had more than 2 developers. Even with "best practices", code ended up having a mix of raw SQL and ORM-style queries, and it was hard to reason about the code.

    Since switching to asyncpg [0] these problems have vanished. It commands a deeper knowledge of actual SQL, but I would argue this knowledge is absolutely necessary and one of the disadvantages of an ORM is that it makes the SQL that is eventually run opaque.

    Not sure if there are equivalents to asyncpg for other RDBMS's.

    [0]: https://github.com/MagicStack/asyncpg

  • Piccolo, an async query builder and ORM for Python
    4 projects | dev.to | 10 Jan 2021
    Asyncio makes a lot of sense when you want high throughput applications. Piccolo is built on top of asyncio and a fast database adapter called asyncpg. You can also use Piccolo within a traditional synchronous app too.

What are some alternatives?

When comparing psycopg2 and asyncpg you can also consider the following projects:

queries - PostgreSQL database access simplified

SQLAlchemy - The Database Toolkit for Python

pymssql - Official home for the pymssql source code.

txpostgres - Twisted wrapper for asynchronous PostgreSQL connections

PostgreSQL - Mirror of the official PostgreSQL GIT repository. Note that this is just a *mirror* - we don't work with pull requests on github. To contribute, please see https://wiki.postgresql.org/wiki/Submitting_a_Patch

awesome-mysql - A curated list of awesome MySQL software, libraries, tools and resources

mysql-python - MySQLdb is a Python DB API-2.0 compliant library to interact with MySQL 3.23-5.1 (unofficial mirror)

Python PG Extras - Python PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.

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

PyMySQL - Pure Python MySQL Client

pgbouncer - lightweight connection pooler for PostgreSQL