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.
pip install vertica-pythonVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python environment to 3.7 or newer. If you must use Python 2, you will need to pin vertica-python to a version prior to 1.3.0, which is not recommended as it is no longer maintained.
Review your authentication configuration. If you were relying on the `crypt` package's functionality, consider migrating to newer, more secure authentication methods supported by Vertica and vertica-python (e.g., OAuth 2.0 or Kerberos if applicable).
Replace `ssl: True/False` in your `conn_info` dictionary with `tlsmode: 'require'`, `tlsmode: 'verify-ca'`, or `tlsmode: 'verify-full'` as appropriate for your security requirements. Ensure any necessary `tls_cafile`, `tls_certfile`, and `tls_keyfile` paths are also provided for certificate verification modes.
After `cursor.execute()`, if you expect results from multiple statements, iterate through them using a loop with `while cursor.nextset():` and then `cursor.fetchall()` for each set.
It is recommended to always call a `fetch` method (e.g., `cursor.fetchall()`) after `cursor.execute()` to ensure all errors are captured, especially when dealing with multiple statements.
Define VARCHAR column lengths with sufficient buffer for multi-byte characters, or consider using `LONG VARCHAR` for potentially large or unpredictable string data. Ensure your Python strings are correctly encoded (vertica-python uses UTF-8 by default for unicode_error='strict').
pip install vertica-python
conn_info = {'host': 'your_host', 'port': 5432, 'user': 'correct_user', 'password': 'correct_password', 'database': 'your_db'}conn_info = {'host': 'your_host', 'port': 5432, 'user': 'user', 'password': 'password', 'database': 'db', 'ssl': True, 'ssl_verify_certificate': False} # Note: ssl_verify_certificate=False bypasses security and is not recommended for production.conn_info = {'host': 'correct_host', 'port': correct_port, 'user': 'user', 'password': 'password', 'database': 'db'} # Verify host, port, server status, and firewall rules.import io
import vertica_python
data_to_copy = 'col1_val\tcol2_val\nnext_row_val1\tnext_row_val2'
with vertica_python.connect(**conn_info) as connection:
cursor = connection.cursor()
with io.StringIO(data_to_copy) as data_stream:
cursor.copy("COPY my_table FROM STDIN DELIMITER E'\\t'", data_stream)
connection.commit()