How to use Vanna with various databases¶
You can use Vanna with any database that you can connect to via Python. Here are some examples of how to connect to various databases.
All you have to do is provide Vanna with a function that takes in a SQL query and returns a Pandas DataFrame. Here are some examples of how to do that.
In [ ]:
import vanna as vn
PostgreSQL¶
In [ ]:
import pandas as pd
import psycopg2
conn_details = {...} # fill this with your connection details
conn_postgres = psycopg2.connect(**conn_details)
def run_sql_postgres(sql: str) -> pd.DataFrame:
df = pd.read_sql_query(sql, conn_postgres)
return df
vn.run_sql = run_sql_postgres
Snowflake¶
We have a built-in function for Snowflake, so you don't need to write your own.
In [ ]:
vn.connect_to_snowflake(account='my-account', username='my-username', password='my-password', database='my-database')
Google BigQuery¶
In [ ]:
from google.cloud import bigquery
import pandas as pd
project_id = 'your-project-id' # replace with your Project ID
client_bigquery = bigquery.Client(project=project_id)
def run_sql_bigquery(sql: str) -> pd.DataFrame:
df = client_bigquery.query(sql).to_dataframe()
return df
vn.run_sql = run_sql_bigquery
Amazon Athena¶
In [ ]:
import pandas as pd
from pyathena import connect
conn_details = {...} # fill this with your connection details
conn_athena = connect(**conn_details)
def run_sql_athena(sql: str) -> pd.DataFrame:
df = pd.read_sql(sql, conn_athena)
return df
vn.run_sql = run_sql_athena
Amazon Redshift¶
In [ ]:
import pandas as pd
import psycopg2
conn_details = {...} # fill this with your connection details
conn_redshift = psycopg2.connect(**conn_details)
def run_sql_redshift(sql: str) -> pd.DataFrame:
df = pd.read_sql_query(sql, conn_redshift)
return df
vn.run_sql = run_sql_redshift
Others¶
You can follow a similar pattern to the others for your database. You just have to provide a vn.run_sql
function that takes in a SQL query and returns a Pandas DataFrame.