📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials PostgreSQL with Python

PostgreSQL with Python

5 min read
psycopg2 connects Python to PostgreSQL. Use %s placeholders — never format them into SQL strings. Use RealDictCursor to get rows as dictionaries. Call conn.commit() after write operations.

PostgreSQL + Python (psycopg2)

pip install psycopg2-binary

import psycopg2
conn = psycopg2.connect(dsn='postgresql://user:pass@localhost/mydb')
cur = conn.cursor()

cur.execute('SELECT * FROM users WHERE age > %s', (25,))
rows = cur.fetchall()

conn.commit()
cur.close()
conn.close()