How to Install psycopg2-binary in Python
psycopg2 - Python-PostgreSQL Database Adapter
pip install psycopg2-binary
What is psycopg2-binary?
psycopg2 - Python-PostgreSQL Database Adapter
Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Its main features are the complete implementation of the Python DB API 2.0 specification and the thread safety (several threads can share the same connection). It was designed for heavily multi-threaded applications that create and destroy lots of cursors and make a large number of concurrent "INSERT"s or "UPDATE"s.
Psycopg 2 is mostly implemented in C as a libpq wrapper, resulting in being both efficient and secure. It features client-side and server-side cursors, asynchronous communication and notifications, "COPY TO/COPY FROM" support. Many Python types are supported out-of-the-box and adapted to matching PostgreSQL data types; adaptation can be extended and customized thanks to a flexible objects adaptation system.
Psycopg 2 is both Unicode and Python 3 friendly.
Quick Start
Minimal example to get started with psycopg2-binary:
import psycopg2
conn = psycopg2.connect(
host="localhost", database="mydb",
user="postgres", password="secret"
)
cur = conn.cursor()
cur.execute("SELECT version();")
print(cur.fetchone())
conn.close()
Installation
pip (standard)
pip install psycopg2-binary
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install psycopg2-binary
pip3
pip3 install psycopg2-binary
conda
conda install -c conda-forge psycopg2-binary
Poetry
poetry add psycopg2-binary
Verify the Installation
After installing, confirm the package is available:
python -c "import psycopg2; print(psycopg2.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing psycopg2-binary with pip.
ModuleNotFoundError: No module named 'psycopg2'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install psycopg2-binary. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'psycopg2' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install psycopg2-binary to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'psycopg2'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show psycopg2-binary and upgrade with pip install --upgrade psycopg2-binary.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install psycopg2-binary. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user psycopg2-binary
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org psycopg2-binary
Runtime Errors
Common errors when using psycopg2-binary after installation.
psycopg2.OperationalError: could not connect to server: Connection refused
Cause: PostgreSQL is not running or not accepting connections.
Fix: Start PostgreSQL and verify host, port, database name, user, and password.
psycopg2.ProgrammingError: relation does not exist
Cause: The table referenced in the SQL query does not exist in this database.
Fix: Run your schema migrations. Confirm you are connected to the right database.
Recent Releases
| Version | Released |
|---|---|
2.9.11 latest |
2025-10-10 |
2.9.10 |
2024-10-16 |
2.9.9 |
2023-10-03 |
2.9.8 |
2023-09-28 |
2.9.7 |
2023-08-05 |
Manage psycopg2-binary
Upgrade to latest version
pip install --upgrade psycopg2-binary
Install a specific version
pip install psycopg2-binary==2.9.11
Uninstall
pip uninstall psycopg2-binary
Check what is installed
pip show psycopg2-binary