Not able to connect to snowflakeDB

217 views Asked by At

this is my code to connect to PG

database = f"postgresql://{db_config.get('user')}:{db_config.get('password')}@{db_config.get('host')}/{db_config.get('database')}"

db_connection_pool = create_engine(database, pool_size=config.get_connection_pool_size(), max_overflow=config.get_connection_overflow_size(), pool_ping=True)

session_maker = sessionmaker(bind=db_connection_pool, autocommit=False)



snowflake connection 

account='',
user='',
password='',
database='',
schema='',
warehouse='',
role='',

database = f"snowflake://{user}:{password}@{account}/{database}/{schema}?warehouse={warehouse}&role={role}"

db_connection_pool = create_engine(database ,pool_size= config.get_connection_pool_size(), max_overflow=config.get_connection_overflow_size(), pool_ping=True)
connection = db_connection_pool.connect()

trying to change it to snowflake (new to it can someone help me)

1

There are 1 answers

0
Dave Welden On
  1. Install the Snowflake Connector for Python:

pip install snowflake-connector-python==<version>

  1. Use this code for the Snowflake database connection:
import snowflake.connector

connection_parameters = {
    "user"          : 'MyUser',
    "password"      : 'MyPassword',
    "account"       : 'MyAccount',
    "role"          : 'MyRole',
    "warehouse"     : 'MyWarehouse',
    "database"      : 'MyDatabase',
    "schema"        : 'MySchema'
}

with snowflake.connector.connect(**connection_parameters) as connection:

    with connection.cursor() as cursor:

        # More python database code

Alternatively, you may want to save the connection parameters in a separate JSON file and use json.load to load them.