Vertica Python Client
vertica-python is the official native Python client for the Vertica Analytics Database. It provides a DB-API 2.0 compliant interface for connecting to and interacting with Vertica, supporting features like query execution, data retrieval, and bulk loading. Currently at version 1.4.0, the library maintains an active development pace with frequent minor releases addressing bug fixes, performance improvements, and new features.
Warnings
- breaking Starting with version 1.3.0, vertica-python dropped support for Python 2.x. The library now explicitly requires Python 3.7 or higher.
- deprecated The `crypt` package, which was used for certain authentication mechanisms, has been deprecated in version 1.3.0. While still functional, users should be aware that its use is discouraged.
- deprecated The `ssl` connection option for enabling TLS/SSL has been deprecated. Users should now use the `tlsmode` connection option for more granular control over TLS security levels (e.g., 'disable', 'require', 'verify-ca', 'verify-full).
- gotcha When executing multiple SQL statements in a single `cursor.execute()` call, only the results from the first statement are immediately accessible. To retrieve results from subsequent statements, you must explicitly call `cursor.nextset()`.
- gotcha Errors occurring in later statements of a multiple-statement query (or some other specific cases) might not raise an exception immediately during `cursor.execute()`. Instead, they might only be raised when `cursor.fetchone()`, `cursor.fetchmany()`, or `cursor.fetchall()` is called.
- gotcha Vertica's VARCHAR length is defined in bytes, not characters. When working with multi-byte UTF-8 characters (e.g., emojis or certain international characters), a VARCHAR(10) might only be able to store a few characters, leading to 'String data right truncation' errors if not accounted for.
Install
-
pip install vertica-python
Imports
- vertica_python
import vertica_python
- connect
from vertica_python import connect
- Error
from vertica_python import Error, DatabaseError, ProgrammingError
Quickstart
import vertica_python
import os
# Configure connection details using environment variables for security
conn_info = {
'host': os.environ.get('VERTICA_HOST', '127.0.0.1'),
'port': int(os.environ.get('VERTICA_PORT', 5433)),
'user': os.environ.get('VERTICA_USER', 'dbadmin'),
'password': os.environ.get('VERTICA_PASSWORD', 'password'),
'database': os.environ.get('VERTICA_DATABASE', 'verticadb'),
'read_timeout': 600, # 10 minutes timeout on queries
'unicode_error': 'strict', # default throw error on invalid UTF-8 results
'ssl': False # SSL is disabled by default, consider 'tlsmode' for production
}
try:
# Using 'with' statement for automatic connection closing
with vertica_python.connect(**conn_info) as connection:
print("Successfully connected to Vertica.")
with connection.cursor() as cursor:
# Execute a DDL statement
cursor.execute("DROP TABLE IF EXISTS my_test_table;")
cursor.execute("CREATE TABLE my_test_table (id INT, name VARCHAR(100));")
print("Table created.")
# Insert data
cursor.execute("INSERT INTO my_test_table (id, name) VALUES (1, 'Alice');")
cursor.execute("INSERT INTO my_test_table (id, name) VALUES (2, 'Bob');")
print("Data inserted.")
# Query data
cursor.execute("SELECT id, name FROM my_test_table;")
rows = cursor.fetchall()
print("Retrieved data:")
for row in rows:
print(f" ID: {row[0]}, Name: {row[1]}")
except vertica_python.Error as e:
print(f"An error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")