When I completed the dbt Fundamentals course, I needed to connect dbt to a Snowflake account.

If you've connected the two platforms before, you may remember being able to authenticate using a username and password. However, this is no longer supported for connections to Snowflake. These now require one of the newer authentication methods, such as key-pair authentication.

What is key-pair authentication?

Instead of authenticating with a password, you create two cryptographic keys:

  • A public key, which is stored in Snowflake.
  • A private key, which remains securely stored on your computer or in dbt.

When dbt connects to Snowflake, it proves it owns the matching private key without ever sending that key to Snowflake. Although it sounds complicated, the setup only takes a few minutes.

Step 1: Install Git

The easiest way to generate the required keys on Windows is by installing Git for Windows, which you can get here. Once installed, launch Git Bash.

Step 2: Generate your key pair

First, create a folder to store your keys and move into it:

Bash
mkdir -p ~/.snowflake
cd ~/.snowflake

Now generate your private key:

Plain text
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8

You'll be asked to create a passphrase. Choose a strong password and make a note of it. You'll need it later when configuring dbt.

After entering your passphrase, Git Bash will create a private key as a file called rsa_key.p8 inside the .snowflake folder.

Next, generate the public key from your private key:

Plain text
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub

You'll be asked to enter the passphrase you just created. Nothing will appear on screen while you type your passphrase. This is normal. Type it as usual and press Enter. If successful, you should see:

Plain text
writing RSA key

You should now have two files:

  • rsa_key.p8 - your private key
  • rsa_key.pub - your public key

You can check that both files are there by running:

Plain text
ls

If you closed Git Bash at any point in this process the files won't disappear. You can simply open Git Bash again and return to the folder with:

Bash
cd ~/.snowflake

Step 3: Add the public key to Snowflake

To display your public key in Git Bash, run:

Bash
cat rsa_key.pub

You'll see something like:

YAML
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...
-----END PUBLIC KEY-----

Copy the key itself, then open Snowflake and run:

SQL
ALTER USER your_username
SET RSA_PUBLIC_KEY='paste_your_public_key_here';

Only copy the key itself.

Do not include:

YAML
-----BEGIN PUBLIC KEY-----

or:

YAML
-----END PUBLIC KEY-----

Once you've run the command successfully, Snowflake is ready to accept connections using your private key.

Step 4: Configure dbt

First, return to Git Bash and display your private key:

Bash
cat rsa_key.p8

This time, copy the entire output, including:

YAML
-----BEGIN ENCRYPTED PRIVATE KEY-----

and:

YAML
-----END ENCRYPTED PRIVATE KEY-----

Your private key should be kept secret. Don't share it with anyone or store it somewhere publicly accessible.

Create a new Snowflake connection (under 'connections' in dbt settings) and complete the usual connection details:

  • Account
  • Username
  • Database
  • Warehouse

For authentication:

  • Select Key Pair Authentication
  • Paste the entire contents of your rsa_key.p8 file into the private key field.
  • Include the BEGIN ENCRYPTED PRIVATE KEY and END ENCRYPTED PRIVATE KEY lines.
  • Enter the passphrase you chose when generating the key.

dbt will securely store your encrypted private key.

Step 5: Test the connection

Click Test Connection. Hopefully everything works first time. If it doesn't, check the format of your Snowflake account identifier. It should be in this format:

Plain text
EXAMPLE01-ACCOUNT1

In particular, if your Snowflake account identifier is displayed with a /, use a - instead when entering it in dbt.

For example:

Plain text
EXAMPLE01/ACCOUNT1

becomes:

Plain text
EXAMPLE01-ACCOUNT1

This can otherwise result in a fairly generic connection error from dbt. I learned this the hard way!

You're Done!

Hopefully you're now connected to Snowflake using key-pair authentication. I wanted to put together a quick, practical guide that walks through the process from start to finish, and I hope it saves you some time when setting up your own connection.