Stay organized with collections
Save and categorize content based on your preferences.
This document provides instructions on how to load a small amount of data in
the CSV file format into Spanner. You can load sample data before
performing a production data migration to test schemas, queries, and applications.
Set a default project on the gcloud CLI using the following command:
gcloudconfigsetprojectPROJECT_ID
Export the source data in the CSV file format. Consider using
pg_dump for
PostgreSQL databases or
mysqldump
for MySQL databases tools to convert your sample data into the CSV
file format.
Load the data into BigQuery by using the following bq commands:
Export data from BigQuery to Spanner by using
the following command:
bq--use_legacy_sql=false'EXPORT DATA OPTIONS( uri="https://spanner.googleapis.com/projects/PROJECT_ID/instances/SPANNER_INSTANCE/databases/SPANNER_DATABASE" format='CLOUD_SPANNER' spanner_options="""{ "table": "SPANNER_TABLE" }""" ) AS SELECT * FROM BQ_DATASET.BQ_TABLE;'
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Load sample data\n\nThis document provides instructions on how to load a small amount of data in\nthe CSV file format into Spanner. You can load sample data before\nperforming a production data migration to test schemas, queries, and applications.\n\nBefore you begin\n----------------\n\n1. [Install the Google Cloud CLI](/sdk/docs/install) or\n use [Cloud Shell](/shell/docs/launching-cloud-shell), which\n has all the necessary tools pre-installed.\n\n2.\n\n To get the permissions that\n you need to export BigQuery data to Spanner,\n\n ask your administrator to grant you the\n following IAM roles on your project:\n\n - Export data from a BigQuery table: BigQuery Data Viewer (`roles/bigquery.dataViewer`)\n - Run an export job: BigQuery User (`roles/bigquery.user`)\n - Write data to a Spanner table: Spanner Database User (`roles/spanner.databaseUser`)\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\nLoad sample data to Spanner\n---------------------------\n\nThe following instructions are performed using the\n[BigQuery reverse ETL](/bigquery/docs/export-to-spanner)\nworkflow and the [Google Cloud CLI](/sdk).\n\n1. Set a default project on the gcloud CLI using the following command:\n\n ```bash\n gcloud config set project PROJECT_ID\n ```\n2. Export the source data in the CSV file format. Consider using\n [`pg_dump`](https://www.postgresql.org/docs/current/app-pgdump.html) for\n PostgreSQL databases or\n [`mysqldump`](https://dev.mysql.com/doc/refman/8.4/en/mysqldump.html)\n for MySQL databases tools to convert your sample data into the CSV\n file format.\n\n | **Note:** If you are working with sample data that's not available in the CSV file format, then consider [batch loading](/bigquery/docs/batch-loading-data) the sample data to BigQuery.\n3. Load the data into BigQuery by using the following `bq` commands:\n\n 1. Create a BigQuery dataset.\n\n ```bash\n bq mk BQ_DATASET\n ```\n 2. Batch load the data into a new BigQuery table.\n\n ```bash\n bq load \\\n --source_format=CSV \\\n --autodetect \\\n --allow_quoted_newlines \\\n BQ_DATASET.BQ_TABLE /path/to/file\n ```\n\n Alternatively, you can batch load the data from a Cloud Storage file. \n\n ```bash\n bq load \\\n --source_format=CSV \\\n --autodetect \\\n --allow_quoted_newlines \\\n BQ_DATASET.BQ_TABLE gs://BUCKET/FILE\n ```\n4. Create a Spanner schema that matches the imported\n \u003cvar translate=\"no\"\u003eBQ_TABLE\u003c/var\u003e by using the following command:\n\n ```bash\n gcloud spanner databases ddl update SPANNER_DATABASE \\\n --instance=SPANNER_INSTANCE \\\n --ddl=\"CREATE TABLE \u003cvar translate=\"no\"\u003eSPANNER_TABLE\u003c/var\u003e ...\"\n ```\n\n For more information,\n see [Update Spanner schema](/spanner/docs/getting-started/gcloud#create-database).\n5. Export data from BigQuery to Spanner by using\n the following command:\n\n \u003cbr /\u003e\n\n ```bash\n bq --use_legacy_sql=false 'EXPORT DATA OPTIONS(\n uri=\"https://spanner.googleapis.com/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/instances/\u003cvar translate=\"no\"\u003eSPANNER_INSTANCE\u003c/var\u003e/databases/\u003cvar translate=\"no\"\u003eSPANNER_DATABASE\u003c/var\u003e\"\n format='CLOUD_SPANNER'\n spanner_options=\"\"\"{ \"table\": \"\u003cvar translate=\"no\"\u003eSPANNER_TABLE\u003c/var\u003e\" }\"\"\"\n ) AS\n SELECT *\n FROM \u003cvar translate=\"no\"\u003eBQ_DATASET\u003c/var\u003e.\u003cvar translate=\"no\"\u003eBQ_TABLE\u003c/var\u003e;'\n \n ```\n\n \u003cbr /\u003e\n\nWhat's next\n-----------\n\n- [Migration overview](/spanner/docs/migration-overview)"]]