How to Connect Python Application to OCI DBCS using Python-Oracledb

Share on:


Pre-requisite: You should have an Oracle Database running on DBCS OCI


What is Python-oracledb?

The python-oracledb driver is a Python programming language extension module allowing Python programs to connect to the Oracle Database. Python-oracledb is the new name for Oracle’s popular cx_Oracle driver.


Python-oracledb Architecture:

The python-oracledb driver enables access to the Oracle Database using either one of two modes. By default, python-oracledb runs in a “thin” mode, which connects directly to the Oracle Database. This mode does not need Oracle Client libraries.

However, some additional features are available when python-oracledb uses them. Python-oracledb applications that load the Oracle Client libraries via an application script runtime option are said to be in “thick” mode.

The database can be on the same machine as Python or it can be remote.

1. Create DBCS in OCI. (as a pre-requisite)

2. Install Python3:

sudo yum install -y python3

I already have installed, it to check the version



3. Install python-oracledb driver:

Use this link for installation: Python for Oracle Linux

sudo yum -y install Oracle Linux-developer-release-el7



sudo yum -y install python3-oracledb



4. Test your installation by launching the Python console and listing the available modules. Where you can see the oracledb module.





5. Create a small Python application script that connects with DBCS PDB.

import oracledb
import os


db_user = os.environ.get('DBAAS_USER_NAME', 'SYSTEM')
db_password = os.environ.get('DBAAS_USER_PASSWORD', '********************')
db_connect = os.environ.get('DBAAS_DEFAULT_CONNECT_DESCRIPTOR', "")

connection = oracledb.connect(user=db_user, password=db_password, dsn=db_connect)
cursor = connection.cursor()
sql = """select name from v$database"""
for r in cursor.execute(sql):

The above script used oracledb.init.oracle_client() which means thick mode. As thick mode used Oracle client libraries.

I used thick mode because DBCS DB uses encryption and encryption is only supported by thick mode.

If you are not using encryption you can use thin mode without Oracle client libraries.

The below link shows Oracle Database features supported by python-oracledb Thin and Thick modes:

Features supported by python-oracledb

Share on:

More from this Author

How to Delete Non CDB or 11g Database or 11g DB Home from ExaCC

How to Delete Non-CDB or 11g Database or 11g DB Home from ExaCC

Objective: How to delete/remove Non-CDB or 11g database or database home from ExaCC   Prerequisite: Should have Non-CDB or 11g home and database on ... Read More

Migrating JD Edwards to OCI The Stock Market Playbook for IT Leaders

Automation – Apply Database Release Update Patch in Oracle 19c using Ansible

Introduction: In this blog, I will show you how to use Ansible to automate Oracle database patching. I applied the latest Oracle 19c RU Patch ... Read More

Back to Top