Vertica Python Client

1.4.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Vertica database, create a table, insert data, and query it using the `vertica-python` client. It leverages environment variables for sensitive connection details and uses a `with` statement for proper connection and cursor management. Error handling is included to catch common database exceptions.

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}")

view raw JSON →