psycopg2 - Python-YugabyteDB Database Adapter
Project description
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.
Documentation
Documentation is included in the doc directory and is available online.
For any other resource (source code repository, bug tracker, mailing list) please check the project homepage.
Installation
Building Psycopg requires a few prerequisites (a C compiler, some development packages): please check the install and the faq documents in the doc dir or online for the details.
If prerequisites are met, you can install psycopg like any other Python package, using pip to download it from PyPI:
$ pip install psycopg2-yugabytedb
or using setup.py if you have downloaded the source package locally:
$ python setup.py build $ sudo python setup.py install
Note - The YugabyteDB Psycopg2 requires Postgresql version 11 or above (preferrably 14)
You can also obtain a stand-alone package for windows, not requiring a compiler or external libraries, by installing the psycopg2-yugabytedb-binary package from PyPI:
$ pip install psycopg2-yugabytedb-binary
YugabyteDB Psycopg2 Features
Yugabyte Psycopg2 driver is a distributed python driver for YSQL built on the PostgreSQL psycopg2 driver. Although the upstream PostgreSQL psycopg2 driver works with YugabyteDB, the Yugabyte driver enhances YugabyteDB by eliminating the need for external load balancers.
It is cluster-aware, which eliminates the need for an external load balancer.
It is topology-aware, which is essential for geographically-distributed applications. The driver uses servers that are part of a set of geo-locations specified by topology keys.
Load balancing
The Yugabyte Psycopg2 driver has the following load balancing features:
Uniform load balancing
In this mode, the driver makes the best effort to uniformly distribute the connections to each YugabyteDB server. For example, if a client application creates 100 connections to a YugabyteDB cluster consisting of 10 servers, then the driver creates 10 connections to each server. If the number of connections are not exactly divisible by the number of servers, then a few may have 1 less or 1 more connection than the others. This is the client view of the load, so the servers may not be well balanced if other client applications are not using the Yugabyte JDBC driver.
Topology-aware load balancing
Because YugabyteDB clusters can have servers in different regions and availability zones, the YugabyteDB JDBC driver is topology-aware, and can be configured to create connections only on servers that are in specific regions and zones. This is useful for client applications that need to connect to the geographically nearest regions and availability zone for lower latency; the driver tries to uniformly load only those servers that belong to the specified regions and zone. The Yugabyte Psycopg2 driver can be configured with pooling as well.
Usage
Load balancing connection properties:
The following connection properties need to be added to enable load balancing:
load_balance - enable cluster-aware load balancing by setting this property to True; disabled by default.
topology_keys - provide comma-separated geo-location values to enable topology-aware load balancing. Geo-locations can be provided as cloud.region.zone.
yb-servers-refresh-interval - The list of servers, to balance the connection load on, are refreshed periodically every 5 minutes by default. This time can be regulated by this property.
Pass new connection properties for load balancing in the connection URL or in the dictionary. To enable uniform load balancing across all servers, you set the load-balance property to True in the URL, as per the following example.
Connection String:
conn = psycopg2.connect("dbname=database_name host=hostname port=port user=username password=password load_balance=true")
Connection Dictionary:
conn = psycopg2.connect(user = 'username', password='xxx', host = 'hostname', port = 'port', dbname = 'database_name', load_balance='True')
To specify topology keys, you set the topology_keys property to comma separated values, as per the following example.
Connection String:
conn = psycopg2.connect("dbname=database_name host=hostname port=port user=username password=password load_balance=true topology_keys=cloud1.region1.zone1,cloud2.region2.zone2")
Connection Dictionary:
conn = psycopg2.connect(user = 'username', password='xxx', host = 'hostname', port = 'port', dbname = 'database_name', load_balance='True', topology_keys='cloud1.region1.zone1,cloud2.region2.zone2')
Multiple topologies can also be passed to the Topology Keys property, and each of them can also be given a preference value, as per the following example.:
conn = psycopg2.connect("host=127.0.0.1 port=5433 user=yugabyte dbname=yugabyte load_balance=True topology_keys=cloud1.region1.zone1:1,cloud2.region2.zone2:2")
The preference value (appended after :) is optional. So it is compatible with previous syntax of specifying cloud placements.
Preference value :1 means primary placement zone(s), value :2 means first fallback, value :3 means second fallback and so on.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 674d43840cb7063ce5589dac831ed726f2fdbc33d510e7a65ec236e46c7f4188 |
|
MD5 | 11df51ad1d85d24a1f00e15d69192a8d |
|
BLAKE2b-256 | e0e74db66677bc10260c957ac25695ee84779fcb5841659fb2b49f8074f21f6a |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f02f75d2038eebdf363259dffeea1409169634ace3c276a492d45e7fadf7272 |
|
MD5 | 0b3295c3496c333f57e33165243a2c55 |
|
BLAKE2b-256 | e389e3c6ae193beb4968f74f224103f88e33db0e7206bc7628a420397b4a0229 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3135cc36396a8d0527be45ecbb4f1424cf1016b0a5d07d90ecb182f7dba050b5 |
|
MD5 | baf8350ac238f8da26ac888b314565c0 |
|
BLAKE2b-256 | 4b2fe6cec207b729292fad361b0b1ec52456e33c06cf50a40727e74690254283 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92d44ea28dec354d6fa0dea03d2efe16db47a57f31c86c3bdd27b6983910fec8 |
|
MD5 | 5eea024806b8fe1950f916799e2e20b5 |
|
BLAKE2b-256 | 02e1eda7391e4fcdcbdfca1916a50f121e54aaf070429d705a10a127d45d48d8 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dabd9d58dd5539b0603d9cb38b9a0d0a46d219856d3b69bd158871eb7fec9f6 |
|
MD5 | e5816335780296c8e0089adb65a76417 |
|
BLAKE2b-256 | 72aa403d9a65552e3a4a07d52da3a59c6047fb2a9daf3985a081a961c68ad4b8 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c39ce8372e65b56a6ce89c93533598a8bea7b5aebca1ca6ae9bfc3fcf5e3717e |
|
MD5 | 79ca23ad2bb07e176a32e4baebae396d |
|
BLAKE2b-256 | 87c0136b767e7352b8f8c23a7a3ab8537d38830131497728969339bc9242528e |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d48b57b7412197bad79528a9e8310bb60e9ee9b5a17667e6dd44f6f72cc2c2d |
|
MD5 | f2248648ec26092f742666d6530cc764 |
|
BLAKE2b-256 | 16913705d7ea9af4feda64d58c05ca0e49ccd70e8a84582ecbce920ece09cb50 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb7bf6ad187cfe15749d129af73ca157fef57a3546e4ef677530721e5a158677 |
|
MD5 | 279eeffb0c383b7671af017112251224 |
|
BLAKE2b-256 | ad42174c5a6ed8b28428f2a1c9b4ce6af09881a65aba99081aba79d27ec58145 |
Hashes for psycopg2_yugabytedb_binary-2.9.3.2b0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0621136f3a52da5449e443a129684168f945c5c7547b47c68ea37726919115ab |
|
MD5 | 2715784050234ef89870b56474460cb2 |
|
BLAKE2b-256 | 3e842cb08fa0d449a2199d22fef70a27294d17131f2560abc195a5ab369210e5 |