Para obtener los permisos que
necesitas para usar bases de datos de Spanner y trabajar con ellas,
pídele a tu administrador que te otorgue los
siguientes roles de IAM:
Crea un proyecto Google Cloud nuevo o haz clic en el nombre del proyecto para abrir uno existente.
…consulta los lineamientos para asignar nombres
Abre una ventana de la terminal y configura tu proyecto como el proyecto predeterminado para Google Cloud CLI. Reemplaza MY_PROJECT_ID por tu ID del proyecto (no el nombre del proyecto):
Nota: Si usas una cuenta de servicio en un proyecto diferente para acceder a tu
instancia de Spanner, debes habilitar la API de Spanner
en ambos proyectos.
Configura la autenticación
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
Instala Google Cloud CLI.
Después de la instalación,
inicializa Google Cloud CLI ejecutando el siguiente comando:
Si deseas configurar gcloud CLI para usar la identidad temporal como cuenta de servicio y autenticarte en las APIs de Google, en lugar de tus credenciales de usuario, ejecuta el siguiente comando:
Para usar código de Terraform en un entorno de desarrollo local, instala e inicializa
gcloud CLI y, luego, configura las credenciales predeterminadas de la aplicación con tus
credenciales de usuario.
Para usar bibliotecas cliente en un entorno de desarrollo local, instala e inicializa
gcloud CLI y, luego, configura las credenciales predeterminadas de la aplicación con tus
credenciales de usuario.
Para configurar tu archivo ADC local con el objetivo de usar la identidad temporal como cuenta de servicio y autenticarte en las
APIs de Google, en lugar de tus credenciales de usuario, ejecuta el siguiente comando:
Para obtener más información, consulta
Autentícate para usar REST
en la documentación de autenticación de Google Cloud .
Puedes usar la identidad temporal como cuenta de servicio y generar un token de acceso para solicitudes a la API de REST.
Para obtener más información, consulta
Cuenta de servicio con identidad temporal.
Ejecuta Google Cloud CLI
Ahora que ya configuraste tu entorno de desarrollo y tu autenticación, ejecuta la herramienta de la línea de comandos de gcloud para interactuar con Spanner:
gcloudspannerinstance-configslist
Deberías ver una lista de las configuraciones de instancias de Spanner a las que puede acceder tu proyecto, incluidas las configuraciones regionales, birregionales y multirregionales. Para obtener más información, consulta la descripción general de las instancias.
Terminaste la configuración.
¿Qué sigue?
Aprende a usar las bibliotecas cliente y los controladores de Cloud para crear una instancia, una base de datos, índices y tablas de Spanner. Luego, almacena, consulta y lee datos en Spanner.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-09-05 (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)"]]