Connect to Spanner
As a BigQuery administrator, you can create aconnection to accessSpanner data. This connection enables data analysts toquerydata in Spanner.
Before you begin
- Enable the BigQuery Connection API.
To get the permissions that you need to connect to Spanner, ask your administrator to grant you theBigQuery Connection Admin (
roles/bigquery.connectionAdmin) IAM role on the project. For more information about granting roles, seeManage access to projects, folders, and organizations.You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Create Spanner connections
Select one of the following options:
Console
Go to theBigQuery page.
In theExplorer pane, clickAdd data.
TheAdd data dialog opens.
In theFilter By pane, in theData Source Type section, selectDatabases.
Alternatively, in theSearch for data sources field, you can enter
Spanner.In theFeatured data sources section, clickGoogle Cloud Spanner.
Click theGoogle Cloud Spanner: BigQuery Federation solution card.
In theExternal data source pane, enter the following information:
- ForConnection type, selectCloud Spanner.
- ForConnection ID, enter an identifier for the connectionresource. Letter, numbers, and underscores are allowed.
- ForLocation type, select a BigQuerylocation (or region) that iscompatible with your external data source region.
- Optional: ForFriendly name, enter a user-friendly name for theconnection, such as
My connection resource. The friendly name canbe any value that helps you identify the connection resource if youneed to modify it later. - Optional: ForDescription, enter a description for thisconnection resource.
- ForDatabase name, enter the name of the Spannerdatabase in the following format:
"projects/PROJECT_ID/instances/INSTANCE/databases/DATABASE" - Optional: To perform parallel reads, selectRead data inparallel. Spanner can divide certain queries intosmaller pieces, or partitions, and fetch the partitions in parallel.For more information, seeRead data in parallelin the Spanner documentation. This option isrestricted to queries whose first operator in the execution plan isadistributed unionoperator. Other queries return an error. To view the query executionplan for a Spanner query, seeUnderstand howSpanner executes queries.
- Optional: ForDatabase role, enter the name of aSpanner database role. If not empty, thisconnection queries Spanner using this database roleby default. Spanner fine-grained access control users who submitqueries through this connectionmust have been granted access to this role by their administrator,and the database role must have the
SELECTprivilege on all schemaobjects specified in external queries. For information aboutfine-grained access control, seeAbout fine-grained access control. - Optional: To enable Data Boost, selectUse Spanner Data Boost.Data Boostlets you execute analytics queries and data exportswith near-zero impact to existing workloads on the provisionedBigQuery instance. To enable Data Boost, selectData Boost andRead data in parallel.
ClickCreate connection.
bq
To create the connection, use thebq mk commandwith the--connection flag.
bqmk--connection\--connection_type=CLOUD_SPANNER\--properties='PROPERTIES'\--location=LOCATION\--display_name='FRIENDLY_NAME'\--description'DESCRIPTION'\CONNECTION_ID
Replace the following:
PROPERTIES: a JSON object with the followingfields:"database": the Spanner database for the connectionSpecify as a string with the following format:
"projects/PROJECT_ID/instances/INSTANCE/databases/DATABASE"."use_parallelism": (Optional) iftrue, this connection performsparallel readsThe default value is
false. Spanner can divide certainqueries into smaller pieces, or partitions, and fetch the partitions inparallel. For more information, seeRead data in parallelin the Spanner documentation. This option is restrictedto queries whose first operator in the execution plan is adistributedunionoperator. Other queries return an error. To view the query executionplan for a Spanner query, seeUnderstand howSpanner executes queries."database_role": (Optional) If not empty, this connection queriesSpanner using this database role by default.Spanner fine-grained access control users who submitqueries through this connectionmust have been granted access to this role by their administrator,and the database role must have theSELECTprivilege on all schemaobjects specified in external queries.If not specified, the connection authenticates withIAM predefined roles for Spanner, andthe principal running queries with this connection must havebeen granted the
roles/spanner.databaseReaderIAMrole.For information aboutfine-grained access control, seeAbout fine-grained access control.
"useDataBoost": (Optional) Iftrue, this connection lets users useData Boost. Data Boost lets users run federated queries in separate, independent, compute capacity distinct from provisioned instances to avoid impacting existing workloads. To enable Data Boost, set"useDataBoost"totrueand"use_parallelism"totrue.In order to use Data Boost, the principal running queries with this connection must have been granted the
spanner.databases.useDataBoostpermission. This permission is included by default in theroles/spanner.adminandroles/spanner.databaseAdminroles.
LOCATION: a BigQuery location thatiscompatible with your external data source region.CONNECTION_ID: an identifier for the connectionresourceThe connection ID can contain letters, numbers and underscores.If you don't provide a connection ID, BigQueryautomatically generates a unique ID.
The following example creates a new connection resource named
my_connection_id.bqmk--connection\--connection_type='CLOUD_SPANNER'\--properties='{"database":"projects/my_project/instances/my_instance/databases/database1"}'\--project_id=federation-test\--location=us\my_connection_id
API
Call theCreateConnection method withintheConnectionService service.
Share connections with users
You can grant the following roles to let users query data and manage connections:
roles/bigquery.connectionUser: enables users to use connections to connectwith external data sources and run queries on them.roles/bigquery.connectionAdmin: enables users to manage connections.
For more information about IAM roles and permissions inBigQuery, seePredefined roles and permissions.
Select one of the following options:
Console
Go to theBigQuery page.
Connections are listed in your project, in a group calledConnections.
In the left pane, clickExplorer:

If you don't see the left pane, clickExpand left pane to open the pane.
Click your project, clickConnections, and then select a connection.
In theDetails pane, clickShare to share a connection.Then do the following:
In theConnection permissions dialog, share theconnection with other principals by adding or editingprincipals.
ClickSave.
bq
You cannot share a connection with the bq command-line tool.To share a connection, use the Google Cloud console orthe BigQuery Connections API method to share a connection.
API
Use theprojects.locations.connections.setIAM methodin the BigQuery Connections REST API reference section, andsupply an instance of thepolicy resource.
Java
Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importcom.google.api.resourcenames.ResourceName;importcom.google.cloud.bigquery.connection.v1.ConnectionName;importcom.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;importcom.google.iam.v1.Binding;importcom.google.iam.v1.Policy;importcom.google.iam.v1.SetIamPolicyRequest;importjava.io.IOException;// Sample to share connectionspublicclassShareConnection{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.StringprojectId="MY_PROJECT_ID";Stringlocation="MY_LOCATION";StringconnectionId="MY_CONNECTION_ID";shareConnection(projectId,location,connectionId);}staticvoidshareConnection(StringprojectId,Stringlocation,StringconnectionId)throwsIOException{try(ConnectionServiceClientclient=ConnectionServiceClient.create()){ResourceNameresource=ConnectionName.of(projectId,location,connectionId);Bindingbinding=Binding.newBuilder().addMembers("group:example-analyst-group@google.com").setRole("roles/bigquery.connectionUser").build();Policypolicy=Policy.newBuilder().addBindings(binding).build();SetIamPolicyRequestrequest=SetIamPolicyRequest.newBuilder().setResource(resource.toString()).setPolicy(policy).build();client.setIamPolicy(request);System.out.println("Connection shared successfully");}}}What's next
- Learn about differentconnection types.
- Learn aboutmanaging connections.
- Learn aboutfederated queries.
- Learn how toquery Spanner data.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.