Connect to AlloyDB for PostgreSQL
As a BigQuery administrator, you can create aconnection to accessAlloyDB data. This connection lets data analystsquerydata in AlloyDB.
To connect to AlloyDB, you must perform the following steps:
Before you begin
- Enable the BigQuery Connection API.
To get the permissions that you need to create an AlloyDB connection, 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 an AlloyDB connection
As a best practice, use connections to handle database credentials when you areconnecting to AlloyDB. Connections are encrypted and stored securely in theBigQuery connection service. If the user credentials are validfor other data in the source, you can reuse the connection. For example, youcan use one connection to query the same database in an AlloyDB instance multiple times.
Select one of the following options to create an AlloyDB connection:
Console
Go to theBigQuery page.
In theExplorer pane, clickAdd.
TheAdd data dialog opens.
In theFilter By pane, in theData Source Type section, selectDatabases.
Alternatively, in theSearch for data sources field, you can enter
alloydb.In theFeatured data sources section, clickGoogle Cloud AlloyDB.
Click theAlloyDB: BigQuery Federation solution card.
In theExternal data source dialog, enter the following information:
- ForConnection type, selectAlloyDB.
- ForConnection ID, enter an identifier for the connectionresource. Letter, numbers, and underscores are allowed. For example,
bq_alloydb_connection. - ForData location, select a BigQuerylocation (or region) that iscompatible with your external data source region.
- Optional: ForFriendly name, enter a user-friendly name forthe connection, such as
My connection resource. The friendly namecan be any value that helps you identify the connectionresource if you need to modify it later. - Optional: ForDescription, enter a description for thisconnection resource.
- Optional:Encryption If you want to use acustomer-managed encryption key (CMEK) to encrypt your credentials, selectCustomer-managed encryption key (CMEK) and then select a customer-managed key. Otherwise, your credentials are protected by the default Google-owned and Google-managed encryption key.
- ForDatabase name, enter the name of the database.
- ForDatabase username, enter the username for the database.
- ForDatabase password, enter the password for the database.
- Optional: To see the password, clickShow password.
ForAlloyDB Instance, enter the connection URI of the AlloyDBprimary or read instance with the//alloydb.googleapis.com prefix.
- Sample URI:
//alloydb.googleapis.com/projects/PROJECT_ID/locations/REGION_ID/clusters/CLUSTER_NAME/instances/INSTANCE_ID
- Sample URI:
ClickCreate connection.
ClickGo to connection.
In theConnection Info pane, copy the service account ID for use in thenext step to grant the correct IAM permissions.
bq
Enter thebq mk commandwith the following flags:
bqmk\--connection\--location=LOCATION\--project_id=PROJECT_ID\--connector_configuration'{ "connector_id": "google-alloydb", "asset": { "database": "DATABASE", "google_cloud_resource": "RESOURCE_PATH" }, "authentication": { "username_password": { "username": "USERNAME", "password": { "plaintext": "PASSWORD" } } } }'\CONNECTION_IDReplace the following:
LOCATION: Specify a region of the BigQuerydataset to be combined with the data from AlloyDB. Queries that usethis connection must be run from this region.PROJECT_ID: Enter your Google Cloud project ID.DATABASE: Enter the database name.RESOURCE_PATH: Enter the connection URI of the AlloyDB primary or read instance with the//alloydb.googleapis.com prefix.- Sample URI:
//alloydb.googleapis.com/projects/PROJECT_ID/locations/REGION_ID/clusters/CLUSTER_NAME/instances/INSTANCE_ID
- Sample URI:
USERNAME: Enter the database user's name.PASSWORD: Enter the database user's password.CONNECTION_ID: Enter a connection ID to identify thisconnection.
Optional flag:
--kms_key_name: A customer-managed encryption key. If omitted, credentials are protected by the default Google-owned and Google-managed encryption key.
API
Within the BigQuery Connection API, you can invokeCreateConnection withintheConnectionService to instantiate a connection. See theclient library page for more details.
Grant access to the service account
Aservice account is automatically created when youcreate the first connection within a project. The service account's name isBigQuery Connection Service Agent. The service account ID is of thefollowing format:
service-PROJECT_NUMBER@gcp-sa-bigqueryconnection.iam.gserviceaccount.com.
To connect to AlloyDB, you must give the new connection access to AlloyDB so that BigQuery can access data on behalf of users. The service account must have the following permission:
alloydb.instances.connect
You can grant the service account associated with the connection theAlloyDB Client IAM role, which already has this permission assigned.You can omit this step if the service account already has the required permission.
Console
Go to theIAM & Admin page.
ClickGrant Access.
TheAdd principals dialog opens.
In theNew principals field, enter the service account nameBigQuery Connection Service Agent or the service account ID taken fromtheconnection information.
In theSelect a role field, selectAlloyDB, and then selectAlloyDB Client.
ClickSave.
gcloud
Use thegcloud projects add-iam-policy-bindingcommand:
gcloud projects add-iam-policy-bindingPROJECT_ID --member=serviceAccount:SERVICE_ACCOUNT_ID --role=roles/alloydb.client
Provide the following values:
PROJECT_ID: Your Google Cloud project ID.SERVICE_ACCOUNT_ID: Replace project number inservice-PROJECT_NUMBER@gcp-sa-bigqueryconnection.iam.gserviceaccount.comand use it.
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.
In theExplorer pane, expand your project name.
ClickConnections and then click the 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
Use the followingset-iam-policy command:
bqset-iam-policyRESOURCEFILE_NAMEReplace the following:
RESOURCE: Enter the resource name in theproject_id.region.connection_idorregion.connection_idformat.FILE_NAME: Enter the filename that contains the IAM policy in a JSON format.
For more information about the set-iam-policy command, seeControl access to resources with IAM.
API
Use theprojects.locations.connections.setIAM methodin the BigQuery Connections REST API reference section andsupply an instance of thepolicy resource.
What's next
- Learn about differentconnection types.
- Learn aboutmanaging connections.
- Learn aboutfederated queries.
- Learn how toquery AlloyDB 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.