psycopg2

Most widely-used PostgreSQL adapter for Python. psycopg2 is a C extension providing fast, full-featured PostgreSQL access with thread safety, server-side cursors, COPY support, and NOTIFY/LISTEN for pub/sub. psycopg3 (new) adds async/await support and type annotations. Used by Django, SQLAlchemy, and most Python PostgreSQL applications. The de facto standard for raw PostgreSQL access in Python.

Evaluated Mar 06, 2026 (0d ago) v2.9+ / psycopg3
Homepage ↗ Repo ↗ Developer Tools python postgresql database sql async open-source
⚙ Agent Friendliness
65
/ 100
Can an agent use this?
🔒 Security
86
/ 100
Is it safe for agents?
⚡ Reliability
88
/ 100
Does it work consistently?

Score Breakdown

⚙ Agent Friendliness

MCP Quality
--
Documentation
85
Error Messages
82
Auth Simplicity
90
Rate Limits
95

🔒 Security

TLS Enforcement
90
Auth Strength
85
Scope Granularity
82
Dep. Hygiene
88
Secret Handling
85

Parameterized queries prevent SQL injection. TLS connections via sslmode parameter. Connection credentials must be in environment variables. Never use string formatting for query construction.

⚡ Reliability

Uptime/SLA
90
Version Stability
90
Breaking Changes
85
Error Recovery
88
AF Security Reliability

Best When

You need direct PostgreSQL access in Python with maximum compatibility, or you're using it as the driver under SQLAlchemy/Django.

Avoid When

You need async PostgreSQL access — use asyncpg for async-native high performance.

Use Cases

  • Connect Python applications to PostgreSQL databases with full protocol support including server-side cursors
  • Execute parameterized SQL queries safely without SQL injection risk
  • Use COPY FROM/TO for high-throughput bulk data loading into PostgreSQL tables
  • Implement PostgreSQL LISTEN/NOTIFY for real-time event notifications from the database
  • Use as the low-level driver under SQLAlchemy, Django ORM, or asyncpg for PostgreSQL connections

Not For

  • Non-PostgreSQL databases — use pymysql for MySQL, sqlite3 for SQLite
  • Async applications with Tokio-style concurrency — use asyncpg or psycopg3 for async PostgreSQL
  • ORM-based development — psycopg2 is a driver; use SQLAlchemy or Django ORM on top

Interface

REST API
No
GraphQL
No
gRPC
No
MCP Server
No
SDK
Yes
Webhooks
No

Authentication

Methods: api_key
OAuth: No Scopes: No

PostgreSQL credentials (user, password, host, dbname) in connection string. SSL certificates for TLS connections.

Pricing

Model: open_source
Free tier: Yes
Requires CC: No

Free and open source.

Agent Metadata

Pagination
cursor
Idempotent
Full
Retry Guidance
Not documented

Known Gotchas

  • psycopg2 uses %s as query placeholder regardless of database — NOT ? (SQLite style) or $1 (asyncpg style); mixing placeholder styles causes syntax errors
  • conn.cursor() creates a cursor but does NOT execute queries — always call cursor.execute(sql, params) with parameterized values, never string interpolation
  • Transactions are open by default — every connection starts in a transaction; call conn.commit() to persist or conn.rollback() to abort; forgetting commit means data isn't persisted
  • psycopg2 connection objects are NOT thread-safe — use a connection pool (psycopg2.pool.ThreadedConnectionPool) for multi-threaded applications
  • psycopg2 requires libpq C library to be installed — in Docker and CI environments, install libpq-dev (apt) or postgresql-libs (apk) before pip install psycopg2
  • psycopg3 (psycopg) is the successor to psycopg2 with different import name — install with pip install psycopg[c]; import as import psycopg, not psycopg2

Full Evaluation Report

Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for psycopg2.

$99

Scores are editorial opinions as of 2026-03-06.

5215
Packages Evaluated
26151
Need Evaluation
173
Need Re-evaluation
Community Powered