Package: psycopg2-yugabytedb
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.DocumentationDocumentation is included in thedocdirectory and isavailable online.For any other resource (source code repository, bug tracker, mailing list)
please check theproject homepage.InstallationBuilding Psycopg requires a few prerequisites (a C compiler, some development
packages): please check theinstalland thefaqdocuments in thedocdir
or online for the details.If prerequisites are met, you can install psycopg like any other Python
package, usingpipto download it fromPyPI:$ pip install psycopg2-yugabytedbor usingsetup.pyif you have downloaded the source package locally:$ python setup.py build
$ sudo python setup.py installNote - The YugabyteDB Psycopg2 requires Postgresql version 11 or above (preferrably 14)Linux/OSX:Windows:YugabyteDB Psycopg2 FeaturesYugabyte 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 balancingThe Yugabyte Psycopg2 driver has the following load balancing features:Uniform load balancingIn 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 balancingBecause 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.UsageLoad 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.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')