Loading...

Connect to a Vertica database

The following tutorial demonstrates a quick and easy way to connect to a Vertica database using VerticaPy. For a full exploration of the connection possibilities in VerticaPy, including auto-connections, see Connection.

Requirements

Before connecting to a database, you must satisfy the following requirements:

  • Have access to a machine with Vertica version 9 or later installed

  • Install Python 3.9 or later on your machine

  • Install VerticaPy on your machine

For more information about these installations, see Getting Started.

Connect to a DB

To connect to a database for the first time, use the new_connection() function, replacing the configuration values with the credentials for your database:

import verticapy as vp

vp.new_connection(
    {
        "host": "12.345.67.89",
        "port": "5433",
        "database": "testdb",
        "password": "XxX",
        "user": "dbadmin",
    },
    name = "Vertica_Connection",
)

The connection is saved to the VerticaPy connection file under the name specified in the name parameter. To reconnect to the database using this connection, run the connect() function with the name of the connection as the argument value:

vp.connect("Vertica_Connection")

To view all available connections, use available_connection().

vp.available_connections()

Out: ['Vertica_New_Connection', 'Vertica_Connection']

If you need to confirm the parameters for a given function, you can also use the help function:

help(vp.new_connection)
Help on function new_connection in module verticapy.connection.write:

new_connection(conn_info: dict, name: str = 'vertica_connection', auto: bool = True, overwrite: bool = True, connect_attempt: bool = True, prompt: bool = False) -> None

Saves the new connection in the VerticaPy
connection file. The information is saved
as plaintext in the local machine.
The function
:py:func:`~verticapy.connection.get_connection_file`
returns the associated connection file
path. If you want a temporary connection,
you can use the
:py:func:`~verticapy.connection.set_connection`
function.

Parameters
----------
conn_info: dict
    ``dictionnary`` containing
    the information to set up
    the connection.

     - database:
        Database Name.
     - host:
        Server ID.
     - password:
        User Password.
     - port:
        Database Port (optional, default: 5433).
     - user:
        User ID (optional, default: dbadmin).

    ...

     - env:
        ``bool`` to indicate whether the user and
        password are replaced by the associated
        environment variables. If ``True``, VerticaPy
        reads the associated environment variables
        instead of writing and directly using the
        username and password.
        For example:
        ``{'user': 'ENV_USER', 'password': 'ENV_PASSWORD'}``

        This works only for the user and password.
        The real values of the other variables are
        stored plaintext in the VerticaPy connection
        file. Using the environment variables hides
        the username and password in cases where the
        local machine is shared.

name: str, optional
    Name of the connection.
auto: bool, optional
    If set to True, the connection
    will become the new auto-connection.
overwrite: bool, optional
    If set to ``True`` and the connection
    already exists, the existing connection
    will be overwritten.
connect_attempt: bool
    If set to False, it will not attempt
    to connect automatically.
prompt: bool, optional
    If set to True, it will open a prompt
    tp ask for ``oauth_refresh_token`` as well as ``client_secret``.

For an interactive start guide, you can use the help_start() function:

vp.help_start()
_static/website/user_guides/introduction/user_guide_installation_help_start.png