> [!WARNING]
>
> **Preview**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).
>
>
> For information about access to this release, see the [access
> request page](https://docs.google.com/forms/d/e/1FAIpQLScL0Tw9FP1UmqHDrnuH91NWwNP-Ezif67rTcRRcqBI2RCrSCA/viewform).

This page describes how to use BigQuery Views for AlloyDB to import data from
BigQuery, including native tables, [BigLake external tables](https://docs.cloud.google.com/bigquery/docs/biglake-intro), and [Apache Iceberg managed tables](https://docs.cloud.google.com/bigquery/docs/iceberg-tables).
[Iceberg](https://en.wikipedia.org/wiki/Apache_Iceberg) is an open table format
for managing and exchanging data.

This page assumes that you have an [AlloyDB cluster and primary instance](https://docs.cloud.google.com/alloydb/docs/cluster-create),
and that you have a BigQuery dataset and tables. For more information,
see [Create datasets](https://docs.cloud.google.com/bigquery/docs/datasets) and [Create and use tables](https://docs.cloud.google.com/bigquery/docs/tables).

## Before you begin

1. [Request access to the BigQueryViews feature for your cluster](https://docs.google.com/forms/d/1NIonWtsMgGbbWuVdFKiSIOnsbZ50UX85mL5fBtrmOlw/edit?resourcekey=0-meZ-Wfu1ABNPUPPNsUatcg#responses) and wait until you receive the enablement confirmation before you follow the instructions on this page.
2. Familiarize yourself with [Supported BigQuery data types and column mappings](https://docs.cloud.google.com/alloydb/docs/reference/supported-bigquery-data-types-column-mappings).
3. Enable the Cloud APIs necessary to create and connect to AlloyDB for PostgreSQL.

   [Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=alloydb.googleapis.com,compute.googleapis.com,cloudresourcemanager.googleapis.com,servicenetworking.googleapis.com,bigquerystorage.googleapis.com)
4. In the **Confirm project** step, click **Next** to confirm the name of the project you are going to make changes to.

5. In the **Enable APIs** step, click **Enable** to enable the following:

   - AlloyDB API
   - Compute Engine API
   - Cloud Resource Manager API
   - Service Networking API
   - BigQuery Storage API

   The Service Networking API is required if you plan to configure
   network connectivity to AlloyDB using a VPC network that
   resides in the same Google Cloud project as AlloyDB.

   The Compute Engine API and Cloud Resource Manager API are required if you plan to
   configure network connectivity to AlloyDB using a VPC
   network that resides in a different Google Cloud project.

### Required roles

To grant read access to the BigQuery Dataset to the
AlloyDB cluster service account, you need the following
permissions:

- BigQuery Data Viewer ([`roles/bigquery.dataViewer`](https://docs.cloud.google.com/bigquery/docs/access-control#bigquery.dataViewer)) or any custom role with permissions `bigquery.tables.get` and `bigquery.tables.getData`. When granted on a table or view, this role provides permissions to read data and metadata from the table or view.
- BigQuery Read Session User ([`roles/bigquery.readSessionUser`](https://docs.cloud.google.com/bigquery/docs/access-control#bigquery.readSessionUser)) or any custom role with permissions `bigquery.readsessions.create` and `bigquery.readsessions.getData`. Provides the ability to create and use read sessions.

## Grant AlloyDB access to the BigQuery dataset

After the BigQuery Views feature is enabled on your
AlloyDB cluster, grant the AlloyDB cluster service
account access to the BigQuery dataset.

To use the gcloud CLI, you can [install and initialize](https://docs.cloud.google.com/sdk/docs/install)
the Google Cloud CLI, or you can use [Cloud Shell](https://docs.cloud.google.com/shell/docs/using-cloud-shell).

1. Open the gcloud CLI. If you don't have the
   gcloud CLI installed,
   [install and initialize](https://docs.cloud.google.com/sdk/docs/install)
   the gcloud CLI, or use
   [Cloud Shell](https://docs.cloud.google.com/shell/docs/using-cloud-shell).

2. Run the
   [`gcloud beta alloydb clusters describe`](https://docs.cloud.google.com/sdk/gcloud/reference/beta/alloydb/clusters/describe)
   command:

       gcloud beta alloydb clusters describe CLUSTER --region=REGION

   Replace the following:
   - `CLUSTER`: the AlloyDB cluster ID.
   - `REGION`: the location of the AlloyDB cluster, for example, `asia-east1`, `us-east1`. See the full list of regions at [AlloyDB locations](https://docs.cloud.google.com/alloydb/docs/locations).

   The output contains a `serviceAccountEmail` field, which is the service
   account for this cluster.
3. [Grant the required permissions](https://docs.cloud.google.com/alloydb/docs/import-bigquery-iceberg-data#required-roles).
   For more information,
   see [Control access to resources with IAM](https://docs.cloud.google.com/bigquery/docs/control-access-to-resources-iam).

   If the cluster service account doesn't have the required permissions, the
   following errors appear when a query is executed against the
   BigQuery table:
   - `The user does not have bigquery.readsessions.create permissions`
   - `Permission bigquery.tables.get denied on table`
   - `Permission bigquery.tables.getData denied on table`

   > [!NOTE]
   > **Note:** If `bigquery.readsessions.getData` isn't provided, empty results are returned without an error.

## Configure the extension

1. Create the extension.

   1. Connect to the AlloyDB instance using the psql client by following the instructions in [Connect a psql client to an instance](https://docs.cloud.google.com/alloydb/docs/connect-psql). Or, you can use AlloyDB Studio. For more information, see [Manage your data using the Google Cloud console](https://docs.cloud.google.com/alloydb/docs/manage-data-using-studio).
   2. Run the following command:

          CREATE EXTENSION bigquery_fdw;

      > [!NOTE]
      > **Note:** You must create this extension in all the databases where BigQuery datasets need to be accessed.

2. Create a foreign server to define the connection parameters for the
   remote BigQuery dataset.

       CREATE SERVER BIGQUERY_SERVER_NAME FOREIGN DATA WRAPPER bigquery_fdw;

   Replace the following:
   - `BIGQUERY_SERVER_NAME`: unique identifier for the foreign server. Define this once in a given database. You can replace `BIGQUERY_SERVER_NAME` with your server name.
3. Create the user mapping by running the `CREATE USER MAPPING` command, which
   specifies the credentials to use when you connect to the foreign server.

       CREATE USER MAPPING FOR USERNAME SERVER BIGQUERY_SERVER_NAME ;

   Replace the following:
   - `USERNAME`: a database username or an IAM user that accesses the foreign table.
   - `BIGQUERY_SERVER_NAME`: unique identifier for the foreign server that you created.
4. Define foreign tables that correspond to the tables that you want to access
   in BigQuery using the `CREATE FOREIGN TABLE` command. This
   command lets you define the structure of a remote table. The
   foreign table can either have all or a subset of the columns in the source
   table in BigQuery.

   > [!NOTE]
   > **Note:** Make sure that the data type that you're using maps to the supported data types. For more information, see [Supported BigQuery data types and column mappings](https://docs.cloud.google.com/alloydb/docs/reference/supported-bigquery-data-types-column-mappings).

       CREATE FOREIGN TABLE TABLENAME (
       COLUMNX_NAME DATA_TYPE,
       COLUMNX_NAME DATA_TYPE,
       ...
       ) SERVER  BIGQUERY_SERVER_NAME OPTIONS (project BIGQUERY_PROJECT_ID, dataset  BIGQUERY_DATASET_NAME, table  BIGQUERY_TABLE_NAME);

   Replace the following:
   - `TABLENAME`: name of the foreign table in the local database.
   - `COLUMNX_NAME`: the column name must match the column name in the BigQuery source.
   - `DATA_TYPE`: the data type of the column.
   - `BIGQUERY_SERVER_NAME`: unique identifier for the foreign server that you created.
   - `BIGQUERY_PROJECT_ID`: ID of the project where the BigQuery dataset resides.
   - `BIGQUERY_DATASET_NAME`: name of the BigQuery dataset for the table.
   - `BIGQUERY_TABLE_NAME`: name of the BigQuery table.

   After the foreign table is created, you can query this table the same way that
   you query any table in AlloyDB.

## Import data

To import BigQuery data or BigLake Iceberg data
to AlloyDB, follow these steps:

1. Identify an existing data source or
   [create a native BigQuery table](https://docs.cloud.google.com/bigquery/docs/tables) or a
   [new Iceberg managed tables](https://docs.cloud.google.com/bigquery/docs/iceberg-tables#create-iceberg-tables).

2. Use [psql](https://docs.cloud.google.com/alloydb/docs/connect-psql)
   to create `local_table` by running the following command:

       CREATE TABLE local_table AS (SELECT * from foreign_table);

   This command creates a copy of the BigQuery table to a
   local, native AlloyDB table.  

   Depending upon your application workflow, you can configure the PostgreSQL
   `pg_cron` extension to refresh the AlloyDB table at periodic
   intervals.

## Set up a schedule to import data to AlloyDB

To set up a schedule to import BigQuery data or BigLake
Iceberg data to AlloyDB, follow these steps:

1. [Configure the `bigquery_fdw` extension](https://docs.cloud.google.com/alloydb/docs/import-bigquery-iceberg-data#configure-extension).
2. Enable the `pg_cron` extension on the AlloyDB instance. For more information, see [Supported database extensions](https://docs.cloud.google.com/alloydb/docs/reference/extensions).
   1. Set the `alloydb.enable_pg_cron` flag to `on`. For more information, see [alloydb.enable_pg_cron](https://docs.cloud.google.com/alloydb/docs/reference/alloydb-flags#alloydb.enable_pg_cron).
   2. Set the `cron.database_name` flag to the database name where you installed the `bigquery_fdw` extension, and where you want to execute the SQL queries for import. For more information, see [Supported database flags](https://docs.cloud.google.com/alloydb/docs/reference/database-flags).
3. To periodically refresh a local copy of the foreign table, run the
   following commands in the database where you installed the `bigquery_fdw`
   extension:

       CREATE EXTENSION pg_cron;
       SELECT cron.schedule(JOB_NAME, SCHEDULE, 'CREATE TABLE IF NOT EXISTS local_table_copy AS (SELECT * FROM foreign_table); DROP TABLE IF EXISTS local_table; ALTER TABLE local_table_copy RENAME TO local_table;');

   Replace the following:
   - `JOB_NAME`: the name of the job.
   - `SCHEDULE`: the schedule for the job.

   For more information, see
   [What is pg_cron?](https://github.com/citusdata/pg_cron/?tab=readme-ov-file#readme).

## What's next

- Learn about [use cases for accessing BigQuery data from AlloyDB](https://docs.cloud.google.com/alloydb/docs/bigquery-view-alloydb-overview).
- [Access BigQuery data from AlloyDB](https://docs.cloud.google.com/alloydb/docs/access-bigquery-data-from-alloydb).
- Learn about [supported BigQuery data types and column mappings](https://docs.cloud.google.com/alloydb/docs/reference/supported-bigquery-data-types-column-mappings).