Back to Yugabyte Db

PostgreSQL Psycopg2 Driver

docs/content/stable/develop/drivers-orms/python/postgres-psycopg2-reference.md

2026.1.0.0-b257.7 KB
Original Source
<ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../yugabyte-psycopg2-reference/" class="nav-link "> </i> Yugabyte Psycopg2 </a> </li> <li > <a href="../postgres-psycopg2-reference/" class="nav-link active"> <i class="icon-postgres" aria-hidden="true"></i> PG Psycopg2 </a> </li> <li > <a href="../postgres-psycopg3-reference/" class="nav-link"> <i class="icon-postgres" aria-hidden="true"></i> PG Psycopg3 </a> </li> </ul>

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). YugabyteDB has full support for Psycopg2. psycopg2 is based on libpq and supports the SCRAM-SHA-256 authentication method.

For details on using psycopg2, see Psycopg documentation.

For a tutorial on building a sample Python application that uses psycopg2, see Connect an application.

Fundamentals

Learn how to perform common tasks required for Python application development using the PostgreSQL Psycopg2 driver.

Download the driver dependency

Building Psycopg2 requires a few prerequisites (a C compiler, some development packages). Check the installation instructions and the FAQ for details.

If prerequisites are met, you can install psycopg like any other Python package, using pip to download it from PyPI:

sh
$ pip install psycopg2

Or, you can use the setup.py script if you've downloaded the source package locally:

sh
$ python setup.py build
$ sudo python setup.py install

You can also obtain a stand-alone package, not requiring a compiler or external libraries, by installing the psycopg2-binary package from PyPI:

sh
$ pip install psycopg2-binary

The binary package is a practical choice for development and testing but in production it is recommended to use the package built from sources.

Connect to YugabyteDB database

Python applications can connect to and query the YugabyteDB database using the following:

  • Import the psycopg2 package.

    python
    import psycopg2
    
  • The Connection details can be provided as a string or a dictionary.

    Connection String

    python
    "dbname=database_name host=hostname port=port user=username  password=password"
    

    Connection Dictionary

    python
    user = 'username', password='xxx', host = 'hostname', port = 'port', dbname = 'database_name'
    

The following table describes the connection parameters required to connect to the YugabyteDB database

ParametersDescriptionDefault
hosthostname of the YugabyteDB instancelocalhost
portListen port for YSQL5433
database/dbnameDatabase nameyugabyte
userUser for connecting to the databaseyugabyte
passwordPassword for the useryugabyte

The following is an example connection string for connecting to YugabyteDB.

python
conn = psycopg2.connect(dbname='yugabyte',host='localhost',port='5433',user='yugabyte',password='yugabyte')

Create a cursor

To execute any SQL commands, a cursor needs to be created after a connection is made. It allows Python code to execute PostgreSQL commands in a database session. Cursors are created by the connection.cursor() method; they are bound to the connection for the entire lifetime, and all the commands are executed in the context of the database session wrapped by the connection.

python
cur = conn.cursor()

Create tables

Tables can be created in YugabyteDB by passing the CREATE TABLE DDL statement to the cursor.execute(statement) method, using the following example:

sql
CREATE TABLE IF NOT EXISTS employee (id int PRIMARY KEY, name varchar, age int, language text)
python
conn = psycopg2.connect(dbname='yugabyte',host='localhost',port='5433',user='yugabyte',password='yugabyte')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS employee (id int PRIMARY KEY, name varchar, age int, language varchar)')

Read and write data

Insert data

To write data into YugabyteDB, execute the INSERT statement using the cursor.execute(statement) method.

For example:

sql
INSERT INTO employee VALUES (1, 'John', 35, 'Java')
python
conn = psycopg2.connect(dbname='yugabyte',host='localhost',port='5433',user='yugabyte',password='yugabyte')
cur = conn.cursor()
cur.execute('INSERT INTO employee VALUES (1, 'John', 35, 'Java')')
<!-- For inserting data using JDBC clients, it is always a good pratice to use `java.sql.PreparedStatemet` for executing `INSERT` statements. ```java Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5433/yugabyte","yugabyte", "yugabyte"); Statment stmt = conn.createStatement(); try { PreparedStatement pstmt = connection.prepareStatement("INSERT INTO employees (id, name, age, langugage) VALUES (?, ?, ?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "John"); pstmt.setInt(3, 35); pstmt.setString(4, "Java"); pstmt.execute(); } catch (SQLException e) { System.err.println(e.getMessage()); } ``` -->

Query data

To query data from YugabyteDB tables, execute the SELECT statement using cursor.execute(statement) method followed by cursor.fetchall() method. fetchall() fetches all the rows of a query result, returning them as a list of tuples. An empty list is returned if there are no records to fetch.

For example:

sql
SELECT * from employee;
python
conn = psycopg2.connect(dbname='yugabyte',host='localhost',port='5433',user='yugabyte',password='yugabyte')
cur = conn.cursor()
cur.execute('SELECT * from employee')
rows = cur.fetchall()
for row in rows:
  print("\nQuery returned: %s, %s, %s" % (row[0], row[1], row[2]))

Configure SSL/TLS

Psycopg2 supports several SSL modes, as follows:

SSL modeClient driver behavior
disableSupported
allowSupported
prefer (default)Supported
requireSupported
verify-caSupported
verify-fullSupported

By default, the driver supports the prefer SSL mode. And in the require mode, a root CA certificate isn't required to be configured.

To enable verify-ca or verify-full, you need to provide the path to the root CA certificate in the connection string using the sslrootcert parameter. The default location is ~/.postgresql/root.crt. If the root certificate is in a different file, specify it in the sslrootcert parameter:

python
conn = psycopg2.connect("host=<hostname> port=5433 dbname=yugabyte user=<username> password=<password> sslmode=verify-full sslrootcert=/Users/my-user/Downloads/root.crt")

The difference between verify-ca and verify-full depends on the policy of the root CA. If you're using a public CA, verify-ca allows connections to a server that somebody else may have registered with the CA. Because of this behavior, you should always use verify-full with a public CA. If you're using a local CA, or even a self-signed certificate, using verify-ca may provide enough protection, but the best security practice is to always use verify-full.

Limitations

Currently, PostgreSQL psycopg2 driver and Yugabyte Psycopg2 smart driver cannot be used in the same environment.