psycopg-binary is the pre-compiled binary distribution for Psycopg 3, a modern PostgreSQL database adapter for Python. It provides C optimizations for performance without requiring local build prerequisites. While `psycopg-binary` is the package name on PyPI, it acts as an optional component for the core `psycopg` library (Psycopg 3). It is actively maintained, currently at version 3.3.3, and follows a frequent release cadence, often aligning with new Python and PostgreSQL versions.
pip install "psycopg[binary,pool]"Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to establish a connection to a PostgreSQL database using Psycopg 3 (installed via `psycopg[binary]`), create a table, insert data, and query it. It uses environment variables for connection details for security and flexibility. The `with` statement ensures proper resource management.
Install using `pip install "psycopg[binary,pool]"` and always `import psycopg` in your code.
For production, consider installing `psycopg[c]` (which requires build tools and `libpq-dev`) or be aware of potential library conflicts. Test thoroughly in your target environment.
Review the official Psycopg 3 documentation for migration guides and adapt your code to the new API. Be especially careful with `SET` statements and date/time handling.
Always check the release notes for the specific `psycopg` (and thus `psycopg-binary`) version for compatible Python versions, and ensure your environment is up-to-date.
Ensure you are using `psycopg-binary` (via `psycopg[binary]`) version 3.1.11 or newer to benefit from memory usage improvements for large queries.
Ensure the PostgreSQL database server is running and accessible from the application's environment. Verify network connectivity, firewall rules, and that the server is configured to listen on the correct host and port (e.g., check `listen_addresses` in `postgresql.conf` and client authentication rules in `pg_hba.conf`).
Ensure your PostgreSQL server is running and configured to accept connections on the specified host and port (e.g., '127.0.0.1', port 5432). Check server logs for startup errors and firewall rules.
Try installing `psycopg` (the source distribution) which requires PostgreSQL development headers and a C compiler, or use a Python version/platform combination for which pre-built `psycopg-binary` wheels exist. For macOS M1/M2, ensure you have the latest `pip` and try installing `psycopg` if `psycopg-binary` fails. You might also need to install PostgreSQL via Homebrew (`brew install postgresql`) and its development libraries.
If you intended to use Psycopg 3, change your import statement from `import psycopg2` to `import psycopg`. If you genuinely need `psycopg2`, you must install it separately via `pip install psycopg2-binary`.
Verify that the PostgreSQL server is running and accessible from the machine running your Python application. Double-check the `host`, `port`, `dbname`, `user`, and `password` in your `psycopg.connect()` call. Ensure no firewall rules are blocking the connection on either the client or server side.
When creating your cursor, specify `cursor_factory=psycopg.ClientCursor` to obtain a client-side cursor that supports the `mogrify` method. ```python import psycopg conn = psycopg.connect(dbname='your_db', user='your_user', password='your_password', host='your_host', cursor_factory=psycopg.ClientCursor) cur = conn.cursor() query = "SELECT * FROM users WHERE id = %s" mogrified_query = cur.mogrify(query, (1,)) print(mogrified_query) ```