Wenn Sie Spanner mit der Google Cloud Console verwenden möchten, finden Sie weitere Informationen unter Schnellstart mit der Konsole.
Erforderliche Rollen
Bitten Sie Ihren Administrator, Ihnen die folgenden IAM-Rollen zuzuweisen, um die Berechtigungen zu erhalten, die Sie zur Verwendung von und Interaktion mit Spanner-Datenbanken benötigen:
Erstellen Sie ein neues Google Cloud Projekt oder öffnen Sie ein vorhandenes Projekt, indem Sie auf den Projektnamen klicken.
...siehe Hinweise zur Namensgebung
Öffnen Sie ein Terminalfenster und legen Sie Ihr Projekt als Standardprojekt für die Google Cloud CLI fest. Dazu ersetzen Sie MY_PROJECT_ID durch Ihre Projekt-ID (nicht durch den Projektnamen):
Hinweis: Wenn Sie ein Dienstkonto in einem anderen Projekt verwenden, um auf Ihre Spanner-Instanz zuzugreifen, müssen Sie die Spanner API in beiden Projekten aktivieren.
Authentifizierung einrichten
Select the tabs for how you plan to access the API:
Console
When you use the Google Cloud console to access Google Cloud services and
APIs, you don't need to set up authentication.
gcloud
Installieren Sie die Google Cloud CLI.
Initialisieren Sie die Google Cloud CLI nach der Installation mit dem folgenden Befehl:
Führen Sie den folgenden Befehl aus, um die gcloud CLI so einzurichten, dass die Authentifizierung bei Google APIs über die Identitätsübernahme des Dienstkontos anstelle Ihrer Nutzeranmeldedaten erfolgt:
Wenn Sie Terraform-Code in einer lokalen Entwicklungsumgebung verwenden möchten, installieren und initialisieren Sie die gcloud CLI und richten Sie dann die Standardanmeldedaten für Anwendungen mit Ihren Nutzeranmeldedaten ein.
Wenn Sie Clientbibliotheken in einer lokalen Entwicklungsumgebung verwenden möchten, installieren und initialisieren Sie die gcloud CLI und richten Sie dann die Standardanmeldedaten für Anwendungen mit Ihren Nutzeranmeldedaten ein.
Führen Sie den folgenden Befehl aus, um die lokale ADC-Datei so einzurichten, dass die Authentifizierung bei Google APIs über die Identitätsübernahme des Dienstkontos anstelle Ihrer Nutzeranmeldedaten erfolgt:
Sie können die Identitätsübernahme des Dienstkontos verwenden, um ein Zugriffstoken für REST API-Anfragen zu generieren.
Weitere Informationen finden Sie unter Dienstkonto mit Identitätsübernahme.
Google Cloud CLI ausführen
Sie haben die Entwicklungsumgebung und die Authentifizierung eingerichtet. Führen nun das gcloud-Befehlszeilentool zur Interaktion mit Spanner aus:
gcloudspannerinstance-configslist
Sie sollten eine Liste der Spanner-Instanzkonfigurationen sehen, auf die Ihr Projekt zugreifen kann, einschließlich regionaler, biregionaler und multiregionaler Konfigurationen. Weitere Informationen finden Sie in der Übersicht über Instanzen.
Sie haben die Einrichtung abgeschlossen.
Nächste Schritte
Erfahren Sie, wie Sie mit den Cloud-Clientbibliotheken und Treibern eine Spanner-Instanz und -Datenbank sowie Tabellen und Indexe erstellen. Speichern und lesen Sie dann Daten in Spanner und rufen Sie sie ab.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-03 (UTC)."],[],[],null,["# Set up your environment\n\nThis page walks you through the setup steps required to use the Cloud Spanner API\nwith the [Google Cloud CLI](/sdk/gcloud), [client\nlibraries](/spanner/docs/reference/libraries), and [Spanner\ndrivers](/spanner/docs/drivers-overview).\n\nIf you want to use Spanner with the Google Cloud console, see\n[Quickstart using the console](/spanner/docs/create-query-database-console).\n\nRequired roles\n--------------\n\n\nTo get the permissions that\nyou need to use and interact with Spanner databases,\n\nask your administrator to grant you the\nfollowing IAM roles:\n\n- Read and write data: [Cloud Spanner Database User](/iam/docs/roles-permissions/spanner#spanner.databaseUser) (`roles/spanner.databaseUser`) on the instance\n- Read-only access to databases: [Cloud Spanner Database Reader](/iam/docs/roles-permissions/spanner#spanner.databaseReader) (`roles/spanner.databaseReader`) on the instance\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nSet up a Google Cloud project\n-----------------------------\n\n1.\n Go to the **Projects** page in the Google Cloud console.\n\n\n [Go to the Projects page](https://console.cloud.google.com/project)\n2. Create a new Google Cloud project, or open an existing project by clicking on the project name.\n| **Important:** The name you use must be between 4 and 30 characters. When you type the name, the form will suggest a project ID, which you can edit. The project ID you use must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash. You should be aware that some resource identifiers (such as project IDs) might be retained beyond the life of your project. For this reason, avoid storing sensitive information in resource identifiers.\n...see naming guidelines\n3.\n Open a terminal window, and set your project as the default project for\n the Google Cloud CLI, replacing\n `MY_PROJECT_ID` with your project ID (not your project name):\n\n gcloud config set project \u003cvar translate=\"no\"\u003eMY_PROJECT_ID\u003c/var\u003e\n\n4. [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\nYou can also [learn about Spanner pricing](/spanner/pricing).\n5.\n Enable the Cloud Spanner API for the project.\n\n\n [Enable the Cloud Spanner API](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com)\n\n\n Note: If you use a service account in a different project to access your\n Spanner instance, you need to enable the Spanner\n API in both projects.\n\nSet up authentication\n---------------------\n\nSelect the tabs for how you plan to access the API: \n\n### Console\n\n\nWhen you use the Google Cloud console to access Google Cloud services and\nAPIs, you don't need to set up authentication.\n\n### gcloud\n\n\n[Install](/sdk/docs/install) the Google Cloud CLI.\n\nAfter installation,\n[initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n```bash\ngcloud init\n```\n\n\nIf you're using an external identity provider (IdP), you must first\n[sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nTo set up the gcloud CLI to use service account impersonation to authenticate to\nGoogle APIs, rather than your user credentials, run the following command: \n\n```bash\ngcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL\n```\n\n\nFor more information, see [Service account impersonation](/spanner/docs/authentication#sa-impersonation).\n\n### Terraform\n\n\nTo use Terraform code in a local development environment, install and initialize the\ngcloud CLI, and then set up Application Default Credentials with your user\ncredentials.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n3. If you're using a local shell, then create local authentication credentials for your user account: \n\n```bash\ngcloud auth application-default login\n```\n4. You don't need to do this if you're using Cloud Shell.\n5. If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Set up authentication for a local development environment](/spanner/docs/authentication#local-development).\n\n### Client libraries\n\n\nTo use client libraries in a local development environment, install and initialize the\ngcloud CLI, and then set up Application Default Credentials with your user\ncredentials.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n3. If you're using a local shell, then create local authentication credentials for your user account: \n\n```bash\ngcloud auth application-default login\n```\n4. You don't need to do this if you're using Cloud Shell.\n5. If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Set up authentication for a local development environment](/spanner/docs/authentication#local-development).\n\n\nTo set up your local ADC file to use service account impersonation to authenticate to\nGoogle APIs, rather than your user credentials, run the following command: \n\n```bash\ngcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL\n```\n\n\nFor more information, see [Service account impersonation](/spanner/docs/authentication#sa-impersonation).\n\n### REST\n\n\nTo use the REST API in a local development environment, you use the credentials you provide to\nthe gcloud CLI.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Authenticate for using REST](/docs/authentication/rest)\nin the Google Cloud authentication documentation.\n\n\nYou can use service account impersonation to generate an access token for REST API requests.\nFor more information, see\n[Impersonated service account](/docs/authentication/rest#impersonated-sa).\n\nRun the Google Cloud CLI\n------------------------\n\nNow that you've set up your development environment and authentication, run the\n[`gcloud` command-line](/spanner/docs/gcloud-spanner) tool to interact with\nSpanner: \n\n gcloud spanner instance-configs list\n\nYou should see a list of the Spanner instance configurations that\nyour project can access, including regional, dual-region, and multi-region\nconfigurations. For more information, see the [Instances\noverview](/spanner/docs/instances).\n\nYou've completed the setup!\n\nWhat's next\n-----------\n\nLearn how to use the Cloud Client Libraries and drivers to create a\nSpanner instance, database, tables, and indexes. Then store,\nquery, and read data in Spanner.\n\n- [Getting started with Spanner in C++](/spanner/docs/getting-started/cpp)\n- [Getting started with Spanner in C#](/spanner/docs/getting-started/csharp)\n- [Getting started with Spanner in Go](/spanner/docs/getting-started/go)\n- [Getting started with Spanner in Java](/spanner/docs/getting-started/java)\n- [Getting started with Spanner in JDBC](/spanner/docs/getting-started/jdbc)\n- [Getting started with Spanner in Node.js](/spanner/docs/getting-started/nodejs)\n- [Getting started with Spanner in PHP](/spanner/docs/getting-started/php)\n- [Getting started with Spanner in Python](/spanner/docs/getting-started/python)\n- [Getting started with Spanner in Ruby](/spanner/docs/getting-started/ruby)\n- [Getting started with Spanner in REST](/spanner/docs/getting-started/rest)\n- [Getting started with Spanner in gcloud](/spanner/docs/getting-started/gcloud)"]]