Skip to main content

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)

Linux/OSX:

Linux and OSX build status

Windows:

Windows build status

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 or any; disabled by default. This section explains the different values for load_balance parameter.

  • 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, is 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=any 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='any', 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.

Other Connection Parameters

  • fallback_to_topology_keys_only - Applicable only for TopologyAware Load Balancing. When set to true, the smart driver does not attempt to connect to servers outside of primary and fallback placements specified via property. The default behaviour is to fallback to any available server in the entire cluster.(default value: false)

  • failed_host_ttl_seconds - The driver marks a server as failed with a timestamp, when it cannot connect to it. Later, whenever it refreshes the server list via yb_servers(), if it sees the failed server in the response, it marks the server as UP only if failed_host_ttl_seconds time has elapsed. (The yb_servers() function does not remove a failed server immediately from its result and retains it for a while.)(default value: 5 seconds)

Read Replica Cluster

psycopg2 smart driver also enables load balancing across nodes in primary clusters which have associated Read Replica cluster.

The connection property load_balance allows five values using which users can distribute connections among different combination of nodes as per their requirements:

  • only-rr - Create connections only on Read Replica nodes

  • only-primary - Create connections only on primary cluster nodes

  • prefer-rr - Create connections on Read Replica nodes. If none available, on any node in the cluster including primary cluster nodes

  • prefer-primary - Create connections on primary cluster nodes. If none available, on any node in the cluster including Read Replica nodes

  • any or true - Equivalent to value true. Create connections on any node in the primary or Read Replica cluster

default value is false

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.12Windows x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

psycopg2_yugabytedb_binary-2.9.3.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9e63c5521e7d7226f0b582ff7afbd6760490239174327ea36df29705b5b338b7
MD5 9bd7c32ea9bdfadfb5ae5130ad93c0de
BLAKE2b-256 552a417c7f661943ae93640c783075c59e7e405f912824d6d5a1c82dd3492198

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f4a293fab721a46dc9ffbf95afddd4cd77367df1cfc511ad3b5c39f0d6dff9a
MD5 2882dcfa7bc9f24068773df12b152f17
BLAKE2b-256 30ed7347689e3ddd9e878af3aa66b8805f6ad56020f43bd04f7f5769466ff361

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6356f7556135a72890f81857cfd2cb6ccb4733551914ba40d6f07bf587fe78cf
MD5 45df736e3ef08aeaafd431c9c6c0d3da
BLAKE2b-256 db33e263ece61819b8b65d06bbb9aa744e6105ef039dc1e48badefc33851ddd8

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8ca4e48fd7661e61828ab1e9a45d2bd10a07eec06ace07f6fca7a199d74fe764
MD5 b1cfdea7ec389da4aa7f6e88505a9dc9
BLAKE2b-256 60f7f21478badfd15ee66a4d463d9c9c333d139243f4b9b34315492648b3e6a1

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 71c6aca809599949d8ec5ff5a605c53de0cce720ec5fb9dcd79795530f361272
MD5 4a830b34e45bcf9c778b639a473bf795
BLAKE2b-256 686d5cb9dc723a5db1998201d2b9accdaf296b6d173f6c6994856a3084346b97

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7134d283e12348d87d9337cc98538dcc11335504aab6ad63c97cba99c3b0e99c
MD5 277f866716e44d15e3e69df5fd5b918a
BLAKE2b-256 861ac9c26d996f4b1e20865cbf7a560a45e8bcd305e5371a14a97eade77f3f69

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4c2b91c400459ead6206be101c9703d1429febfd0616f4290657e645f2cb57d7
MD5 fb5d00bccc1dfec23871434fef5dfaf0
BLAKE2b-256 fefca52face974841197acf38b02092ffef9f73331296afed6ecfef672b8ef56

See more details on using hashes here.

File details

Details for the file psycopg2_yugabytedb_binary-2.9.3.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for psycopg2_yugabytedb_binary-2.9.3.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f2af523841c0da66d17e5be781587c4e7aa5eba9bae898ba5a4aab9072a1eb1b
MD5 5ea0252425c59f411373eaa5c92a507a
BLAKE2b-256 869658c0ba6033ae79573be9fd9ad5743e9bfb412946b4bd381d3f71ed63c460

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page