Google Cloud AlloyDB

0.9.0 · active · verified Thu Apr 09

The `google-cloud-alloydb` library is the official Python client for interacting with Google Cloud AlloyDB, a fully managed, PostgreSQL-compatible database service. It provides programmatic access to manage clusters, instances, backups, and users within AlloyDB. The current version is 0.9.0, indicating it is still in active development, with releases typically tied to updates in the underlying Google Cloud API.

Warnings

Install

Imports

Quickstart

This quickstart initializes an AlloyDB client and lists all clusters within a specified Google Cloud project and region. AlloyDB is a regional service, so specifying the `location` is critical for most operations. Ensure `GOOGLE_CLOUD_PROJECT` environment variable is set or replace the placeholder. Authentication is typically handled automatically via `gcloud auth application-default login` or by setting `GOOGLE_APPLICATION_CREDENTIALS`.

import os
from google.cloud.alloydb_v1 import AlloyDBClient

project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-project-id")
location = "us-central1" # Replace with your desired region

if project_id == "your-project-id":
    print("Please set the GOOGLE_CLOUD_PROJECT environment variable or replace 'your-project-id' in the code.")
    exit()

client = AlloyDBClient()

parent = f"projects/{project_id}/locations/{location}"

try:
    # Listing clusters within a specific region. AlloyDB is a regional service.
    print(f"Fetching AlloyDB Clusters in {location} for project {project_id}...")
    clusters_iterator = client.list_clusters(parent=parent)
    
    found_clusters = []
    for cluster in clusters_iterator:
        found_clusters.append(cluster)
        print(f"- Cluster: {cluster.name} (State: {cluster.state.name}, Type: {cluster.cluster_type.name})")
    
    if not found_clusters:
        print("  No clusters found in this location for the specified project.")

except Exception as e:
    print(f"\nAn error occurred: {e}")
    print("Ensure you have authenticated (e.g., via `gcloud auth application-default login`) ")
    print("and possess the 'AlloyDB Admin' role or equivalent permissions on the project.")
    print("Also, verify that the GOOGLE_CLOUD_PROJECT environment variable and location are correct.")

view raw JSON →