psycopg2 - Python-PostgreSQL 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)
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
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40004515af76effbc3879ac859b97f3c4285919572dba8371467bb5d4280542f |
|
MD5 | 128070de7f4a679572721f9b509f733e |
|
BLAKE2b-256 | 80994fff1dbd180efcf3293aabd1a06aa9a7e45ac71168b14f6599085efe8c06 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7068aafb652517cfb1f33d264e3ea46426d4d2133139fe84496cd119d25c6281 |
|
MD5 | f1aaba989e308f64905c0ad017e594fe |
|
BLAKE2b-256 | ad99340ae87165fee373558f1a1e32db013ef4839751f598bb3d1fbad1b8bb19 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b6d59028cfc8f4950dafb3cc364c1ffc266d36fe726c34a697f848a44d14328 |
|
MD5 | a477f3e1fc9585b3ca461b3dddbc02a7 |
|
BLAKE2b-256 | 740006deaa5142792a1f21a20ef639444a52ddb29ce4dea9e6d59d537e8aabb9 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3025d93a64c29f9596d0ce480ee726eafb1ebb5ac034831222ea2dc7d12e8d5c |
|
MD5 | 52225056f81aa469f241d21e331bf7ca |
|
BLAKE2b-256 | aea8866065c8e2d9c96efaf1ce5b28067ee64f9e34a408e38f676d0ddb66da30 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97cd46377aace27e5eb36f32042b9e19619a97ee6d9996977305a9585ee9b0a2 |
|
MD5 | de7f934f5c026e80302976f2d344cd3e |
|
BLAKE2b-256 | 6414bddeb5cd8c426c698df0d310d4637ef434a075751b8365e36ac3e3078271 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6daec5e264ac623105a63b7037cd72eae8fabe532e1a02848f66e129a217c9f1 |
|
MD5 | 9da727cab8f09e4af3593b174043478e |
|
BLAKE2b-256 | 9296aef26d77fe7e7bfa376e394f693a4967e44fb32f368e07c9a2e1f84c2776 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c41f14493be2d8549522225727c3788274c20e1e8931350837bd2b2d5980f64f |
|
MD5 | 50c276ddfc6bbf6d9720534df544b8e4 |
|
BLAKE2b-256 | 54bf0586108614880749283a2398d7a5b5f8d77a9f6c24f04fcf7008ec9a99b4 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.7m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ec8ef485c80ee1e60d930b7fd2bf11bb3f3b413abc037ddf656af9b45bcb649 |
|
MD5 | c996a2bfea9e9825fa120c51253126ac |
|
BLAKE2b-256 | b88d62958c68d15398fa8add4f24ef2c2b785631ae6a6b90ef81e85bebea96d8 |
File details
Details for the file psycopg2_yugabytedb_binary-2.9.3.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: psycopg2_yugabytedb_binary-2.9.3.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.6m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dba0d4f098f226710bf7d27952e5d4e80104efaae7c8216d81e8560bb6e74df |
|
MD5 | 13b5e2eb7694bd7d4933d9a1556a00e5 |
|
BLAKE2b-256 | debadfb858fab68f4bc7f6285dfe7a18d9e2b8819ed8e6ddde36cdd7f9f23531 |