> ## Documentation Index
> Fetch the complete documentation index at: https://statsig-4b2ff144-kill-dead-node-pages.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Snowflake

## Overview

To set up connection with Snowflake, Statsig needs the following

* Account Name
* Database Name
* Schema Name
* Admin User Name
* If authenticating via login credentials:
  * Admin User Password
* If authenticating via key-pair authentication:
  * Private Key
  * Private Key Passphrase (Optional)

<b>
  Admin user name and password will be used by Statsig to create a user with restricted access to query from your data warehouse. If you don't want to use this, skip ahead [here](/data-warehouse-ingestion/snowflake#custom-user-privileges)
</b>

### Account Name

For the Account Name field, please enter in the format of `<id>.<region>.<provider>`. So, your information may look something like this:

`xy12345.us-central1.gcp`

To get this information navigate to bottom left in your Snowflake console, as shown in the picture below and copy the link URL:

<Frame>
  <img src="https://user-images.githubusercontent.com/108023879/187517221-4bb3dce3-8b8f-4f30-b4d4-fd12e5249722.png" alt="Frame 6" />
</Frame>

The copied URL will look something like this:

`https://xy12345.us-central1.gcp.snowflakecomputing.com`

You can extract information from here to get the required fields for Account Name, which for this example would be `xy12345.us-central1.gcp`.

<Info>
  Using `<orgname>-<account_name>` for Account Name

  For the Account Name field, you can also enter your Snowflake [account identifier](https://docs.snowflake.com/en/user-guide/admin-account-identifier.html#format-1-preferred-account-name-in-your-organization), which typically takes the form `<orgname>-<account_name>`. To find the `<orgname>` in the Snowflake console, click on your account profile (usually at the bottom left) to view account details as shown below.

  <Frame>
    <img src="https://user-images.githubusercontent.com/1315028/195217037-ad630f37-a8fe-4b61-823f-ce0e8c984ed0.png" alt="Snowflake account profile interface" />
  </Frame>
</Info>

### Database and Schema Name

For each data type, provide the database/schema of the table(s) you will be ingesting from.

<Frame>
  <img src="https://user-images.githubusercontent.com/108023879/187517225-017b4626-eaea-443b-a042-59fd474ae657.png" alt="Frame 7" />
</Frame>

### Key-Pair Authentication

To set up key-pair authentication, first follow the [snowflake documentation](https://docs.snowflake.com/en/user-guide/key-pair-auth) to generate the private and public keys, and then set the public key on the service user.

The private key can then be provided here

<Frame>
  <img src="https://github.com/user-attachments/assets/8bb56be6-1aca-4d8a-98de-6b82d251d59b" alt="Private key authentication configuration interface" />
</Frame>

### Custom User Privileges

To create a custom user with specific privileges instead of using an admin user, run the following code in your Snowflake worksheet that has sysadmin and securityadmin roles.
Replace `<USER>` and `<PASSWORD>` with your value, which you will copy over into our console.

```sql theme={null}
BEGIN;

  -- set up variable values to be used in statements later
  -- make sure to configure user_name and user_password with your own values
  SET user_name = '<USER>'; -- REPLACE WITH YOUR OWN VALUE
  SET user_password = '<PASSWORD>'; -- REPLACE WITH YOUR OWN VALUE
  SET role_name = 'STATSIG_ROLE'; -- CAN BE ANYTHING, BUT THE USER NEEDS TO 
    -- HAVE THIS ROLE AND THE ROLE NEEDS ACCESS TO THE TABLES PER THE GRANTS BELOW

  -- change role to sysadmin for warehouse / database steps
  USE ROLE sysadmin;

  -- create a warehouse, database, schema and tables for Statsig
  CREATE OR REPLACE WAREHOUSE STATSIG_INGESTION WITH warehouse_size='XSMALL';
  CREATE DATABASE IF NOT EXISTS STATSIG_STAGING;

  -- change current role to securityadmin to create role and user for Statsig's access
  USE ROLE securityadmin;

  -- create role for Statsig
  CREATE ROLE IF NOT EXISTS identifier($role_name);
  GRANT ROLE identifier($role_name) TO ROLE SYSADMIN;

  -- create a user for Statsig
  CREATE USER IF NOT EXISTS identifier($user_name)
  password = $user_password
  default_role = $role_name
  default_warehouse = STATSIG_INGESTION;
  GRANT ROLE identifier($role_name) TO USER identifier($user_name);

  -- grant Statsig role access to create warehouse and schema
  GRANT USAGE ON WAREHOUSE STATSIG_INGESTION TO ROLE identifier($role_name);
  GRANT CREATE SCHEMA, MONITOR, USAGE ON DATABASE STATSIG_STAGING TO ROLE identifier($role_name);

  -- grant Statsig role read access to database and schema passed in
  GRANT USAGE ON DATABASE <DATABASE> TO ROLE identifier($role_name);
  GRANT USAGE ON SCHEMA <DATABASE>.<SCHEMA> TO ROLE identifier($role_name);
  GRANT SELECT ON ALL TABLES IN DATABASE <DATABASE> TO ROLE identifier($role_name);
  GRANT SELECT ON FUTURE TABLES IN DATABASE <DATABASE> TO ROLE identifier($role_name);
  GRANT SELECT ON ALL VIEWS IN DATABASE <DATABASE> TO ROLE identifier($role_name);
  GRANT SELECT ON FUTURE VIEWS IN DATABASE ACTUAL_DATA TO ROLE identifier($role_name);

COMMIT;
```

After running the script, input the `<USER>` and `<PASSWORD>` you created in our console, during Connection Set Up stage under the Advanced settings options.

<Frame>
  <img src="https://user-images.githubusercontent.com/108023879/188943178-6b541962-62da-4529-b5af-ff59b20b7a8d.png" alt="Screen Shot 2022-09-07 at 10 36 57 AM" />
</Frame>
