> ## Documentation Index
> Fetch the complete documentation index at: https://docs.prequel.co/llms.txt
> Use this file to discover all available pages before exploring further.

# BigQuery

> Connect your BigQuery data warehouse as a source

This connection relies on two service accounts. You create a service account in your own GCP project that holds the BigQuery read permissions described below. Our service account is then granted permission to impersonate yours using short-lived tokens. As a result, no private key is ever shared, every action appears in your own audit logs, and you can revoke access at any time through your IAM settings.

> [!NOTE]
>
> The recommended authentication method, service account impersonation, is covered in Step 3. If your policy requires a different approach, that step also describes how to use a service account key.

## Step 1: create a service account

**1. Open the Service Accounts menu**

In the GCP console, in the same project as your BigQuery data, navigate to the **IAM & Admin** menu, open the **Service Accounts** tab, and click **Create service account**.

![Create service account menu](https://storage.googleapis.com/prequel_docs/images/gcp-create-service-account-menu.png)

**2. Name the service account**

Give the service account a descriptive name and click **Create and Continue**.

![Service account name options](https://storage.googleapis.com/prequel_docs/images/gcp-service-account-name-options.png)

**3. Grant the BigQuery User role**

Grant the new service account the **BigQuery User** role. This allows it to run the read queries that extract your data.

![Grant BigQuery User role](https://storage.googleapis.com/prequel_docs/images/gcp-bigquery-user.png)

**4. Finish and note the email**

Click **Done** to finish creating the account. Open the service account in the list and make a note of its **email**, which looks like `service-account-name@project-id.iam.gserviceaccount.com`. You will provide this email in Step 5.

## Step 2: grant read access to the source dataset

**1. Open the dataset sharing settings**

In **BigQuery**, click on the dataset you want to read from. In the menu on the right, click **Sharing** and then click **Permissions**.

![BigQuery dataset sharing permissions](https://storage.googleapis.com/prequel_docs/images/bigquery-sharing-permissions.png)

**2. Add the service account as a principal**

Click **Add Principal**.

![BigQuery add principal](https://storage.googleapis.com/prequel_docs/images/bigquery-add-principal.png)

**3. Grant the BigQuery Data Viewer role**

Add the service account you created in Step 1, grant it the **BigQuery Data Viewer** role, and click **Save**.

![BigQuery Data Viewer role](https://storage.googleapis.com/prequel_docs/images/bigquery-user-data-viewer.png)

> [!TIP]
>
> If you provide a timestamp column for change tracking, ensure that its values increase in commit order. **Why:** A row committed later with an older timestamp can be skipped.

## Step 3: authorize authentication

Choose the authentication method that fits your security policy. Service account impersonation is recommended because it avoids sharing any long-lived credentials.

> [tab] Service account impersonation (recommended)
>
> **1. Open the service account permissions**
>
> In the **IAM & Admin** menu, open the **Service Accounts** tab and click the service account you created in Step 1 to open its details. Navigate to the **Principals with access** tab and click **Grant Access**.
>
> **2. Grant permission to impersonate**
>
> Add our service account as a principal and grant it both the **Service Account Token Creator** and **Service Account User** roles. Reach out to your account representative for the exact service account email to add.
>
> ![Grant role to the service account](https://storage.googleapis.com/prequel_docs/images/gcp-grant-role.png)

> [tab] Service account key
>
> > [!WARNING]
> >
> > A service account key is a long-lived credential. Use this method only when your policy prevents impersonation, and store the key securely.
>
> **1. Open the key menu**
>
> In the **Service accounts** list, click the **Actions** menu next to the service account from Step 1 and click **Manage keys**.
>
> ![Manage service account keys](https://storage.googleapis.com/prequel_docs/images/gcp-manage-service-account-keys.png)
>
> **2. Create a new key**
>
> Click **Add key** and then **Create new key**.
>
> ![Create new key](https://storage.googleapis.com/prequel_docs/images/gcp-create-new-key.png)
>
> **3. Download the JSON key**
>
> Select the **JSON** key type and click **Create**. Store the downloaded key securely, as you will provide its contents in Step 5.
>
> ![Create JSON key](https://storage.googleapis.com/prequel_docs/images/gcp-create-json-key.png)

## Step 4: find your project ID and data location

**1. Find the Project ID**

In the Google Cloud console, open the projects list dropdown and make a note of your BigQuery **Project ID**.

![GCP Project ID](https://storage.googleapis.com/prequel_docs/images/gcp-project-id.png)

**2. Find the data location**

Open your source dataset in BigQuery and make a note of its **Data location**, such as `us` or `us-central1`. You will provide this value as the region in Step 5.

> [!NOTE]
>
> This connection supports Google Cloud organization policies that restrict identities by domain. If your organization enforces domain-restricted sharing, you can add our principal to your allow list following Google's guidance on [restricting identities by domain](https://cloud.google.com/resource-manager/docs/organization-policy/restricting-domains). Contact your account representative for the customer ID to add.

## Step 5: submit your connection details

Provide the following details to complete the source setup:

1. The **name** is a descriptive name of the source.
2. The **Project ID** from Step 4.
3. The **region** (the data location) from Step 4.
4. The authentication credentials from Step 3:
   * If using **impersonation**: the **service account email** from Step 1.
   * If using a **service account key**: the **service account email** from Step 1 and the **JSON key** you downloaded.

## Terraform

If your organization manages infrastructure with Terraform, you can use the configuration below instead of the steps above. It sets up the same resources and permissions, using service account impersonation.

* Use the official [`hashicorp/google`](https://registry.terraform.io/providers/hashicorp/google/latest/docs) provider. The project needs the IAM API enabled.
* In `terraform.tfvars`, set `project_id`, `dataset_id` (an existing dataset), and `vendor_service_account_email` (our service account email, which your account representative provides). The optional `service_account_id` input defaults to `transfer-source`.
* After `terraform apply`, use the `project_id`, `region`, and `service_account_email` outputs to complete the connection setup. Print each with `terraform output -raw <name>`.

```hcl title="main.tf" icon="code" expandable theme={null}
variable "project_id" {
  type        = string
  description = "GCP project that holds the source dataset."
}

variable "dataset_id" {
  type        = string
  description = "Existing dataset to read from."
}

variable "vendor_service_account_email" {
  type        = string
  description = "The service account email provided for the source; it impersonates the source service account."
}

variable "service_account_id" {
  type    = string
  default = "transfer-source"
}

data "google_bigquery_dataset" "source" {
  project    = var.project_id
  dataset_id = var.dataset_id
}

resource "google_service_account" "source" {
  project      = var.project_id
  account_id   = var.service_account_id
  display_name = "Source service account"
}

resource "google_project_iam_member" "bigquery_user" {
  project = var.project_id
  role    = "roles/bigquery.user"
  member  = "serviceAccount:${google_service_account.source.email}"
}

resource "google_bigquery_dataset_iam_member" "data_viewer" {
  project    = var.project_id
  dataset_id = data.google_bigquery_dataset.source.dataset_id
  role       = "roles/bigquery.dataViewer"
  member     = "serviceAccount:${google_service_account.source.email}"
}

resource "google_service_account_iam_member" "token_creator" {
  service_account_id = google_service_account.source.name
  role               = "roles/iam.serviceAccountTokenCreator"
  member             = "serviceAccount:${var.vendor_service_account_email}"
}

resource "google_service_account_iam_member" "service_account_user" {
  service_account_id = google_service_account.source.name
  role               = "roles/iam.serviceAccountUser"
  member             = "serviceAccount:${var.vendor_service_account_email}"
}

output "project_id" {
  value = var.project_id
}

output "service_account_email" {
  value = google_service_account.source.email
}

output "region" {
  value = lower(data.google_bigquery_dataset.source.location)
}
```

```hcl title="terraform.tfvars" icon="code" expandable theme={null}
project_id                   = "my-project"
dataset_id                   = "my_dataset"
vendor_service_account_email = "some-name@some-project.iam.gserviceaccount.com"
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.