[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-17 (世界標準時間)。"],[],[],null,["# Spanner CLI quickstart\n\n| **Preview\n| --- [Spanner CLI](/spanner/docs/spanner-cli)**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\n\n| **PostgreSQL interface note:** For PostgreSQL-dialect databases, you can use the [psql command-line tool](/spanner/docs/psql-commands). The examples in this document are intended for GoogleSQL-dialect databases.\n\n\u003cbr /\u003e\n\nThis page introduces Spanner CLI and explains how to use it.\n\nThe Spanner CLI is a command-line interface (CLI) that lets you connect and\ninteract with your Spanner database. It's incorporated into the\n[Google Cloud CLI (gcloud CLI)](/sdk/gcloud) to\ninterface with Spanner. You can use the Spanner CLI to\ndirectly execute [GoogleSQL](/spanner/docs/reference/standard-sql/overview)\nstatements in your Spanner database. Your statements can\nconsist of data definition language (DDL), data manipulation language\n(DML), or data query language (DQL) statements. You can use the Spanner CLI\nto run SQL command scripts and automate tasks.\n\nFor more information, see a list of all supported\n[`gcloud alpha spanner cli` commands](/sdk/gcloud/reference/alpha/spanner/cli).\n\nKey benefits\n------------\n\nThe key benefits of the Spanner CLI include:\n\n- Runs DDL, DML, and DQL SQL commands.\n- You can write and execute SQL statements across multiple lines.\n- Supports [meta-commands](#supported-meta-commands) to help you with system tasks such as executing a system shell command and executing SQL from a file.\n- You can automate SQL executions by [writing a series of SQL statements into a script file](#use-file-based), and then instructing Spanner CLI to execute the script. In addition, you can redirect the output to an output file.\n- You can [start an interactive Spanner CLI session](#start-interactive-session), which lets you directly type SQL statements and meta-commands and see results in the CLI.\n\nBefore you begin\n----------------\n\nThe Spanner CLI is available in the gcloud CLI. When you run\nthe `gcloud alpha spanner cli` command for the first time, gcloud CLI\nautomatically installs the Spanner CLI component.\n\nUse the Spanner CLI\n-------------------\n\n1. [Set up a Google Cloud project](/spanner/docs/getting-started/set-up#set_up_a_project).\n\n2. [Set up authentication using the gcloud CLI](/spanner/docs/getting-started/set-up#gcloud).\n\n3. [Create an instance](/spanner/docs/getting-started/gcloud#create-instance).\n\n4. [Create a database](/spanner/docs/getting-started/gcloud#create-database).\n\n5. Run the following command to start the Spanner CLI and interact with your\n Spanner database:\n\n gcloud alpha spanner cli \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e --instance=\u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e: the ID of the Spanner database. This is the name you used in the previous Create a database step. You can use the [`gcloud spanner databases list`](/sdk/gcloud/reference/spanner/databases/list) command to list the Spanner databases contained within the given instance.\n - \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e: the ID of the Spanner instance. This is the name you used in the previous Create an instance step. You can use the [`gcloud spanner instances list`](/sdk/gcloud/reference/spanner/instances/list) command to list the Spanner instances contained within the given project.\n\nExecute SQL\n-----------\n\nYou can execute SQL statements in the Spanner CLI by using the\n[`execute` option](#use-execute-option) or using a\n[file-based input and output method](#use-file-based). Your SQL statements can\nconsist of DDL, DML, or DQL.\n\n### Use the `execute` flag\n\nTo use the `execute` flag to execute SQL, run the following\n[`gcloud alpha spanner cli`](/sdk/gcloud/reference/alpha/spanner/cli) command: \n\n gcloud alpha spanner cli \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e --instance \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e \\\n --execute \"\u003cvar translate=\"no\"\u003eSQL\u003c/var\u003e\"\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e: the ID of the Spanner database that you want to connect to.\n- \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e: the ID of the Spanner instance that you want to connect to.\n- \u003cvar translate=\"no\"\u003eSQL\u003c/var\u003e: the SQL that you want to execute.\n\nFor example, to execute a DDL statement: \n\n gcloud alpha spanner cli test-database --instance test-instance \\\n --execute \"CREATE TABLE Singers ( \\\n SingerId INT64 NOT NULL, \\\n FirstName STRING(1024), \\\n LastName STRING(1024), \\\n SingerInfo STRING(1024), \\\n BirthDate DATE \\\n ) PRIMARY KEY(SingerId);\"\n\nTo execute a DML statement: \n\n gcloud alpha spanner cli test-database --instance test-instance \\\n --execute \"INSERT INTO Singers (SingerId, FirstName, LastName, SingerInfo) \\\n VALUES(1, 'Marc', 'Richards', 'nationality: USA'), \\\n (2, 'Catalina', 'Smith', 'nationality: Brazil'), \\\n (3, 'Andrew', 'Duneskipper', NULL);\"\n\n### Use a file-based input and output\n\nIf you use the file-based input and output method, Spanner reads\nits input from a file and writes its output to another file. To use the\nfile-based input and output method to execute SQL, run the following command: \n\n gcloud alpha spanner cli \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e --instance \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e \\\n --source \u003cvar translate=\"no\"\u003eINPUT_FILE_PATH\u003c/var\u003e --tee \u003cvar translate=\"no\"\u003eOUTPUT_FILE_PATH\u003c/var\u003e\n\nYou can also use the file-based redirection input and output method: \n\n gcloud alpha spanner cli \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e --instance \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e \\\n \u003c \u003cvar translate=\"no\"\u003eINPUT_FILE_PATH\u003c/var\u003e \u003e \u003cvar translate=\"no\"\u003eOUTPUT_FILE_PATH\u003c/var\u003e\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e: the ID of the Spanner database that you want to connect to.\n- \u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e: the ID of the Spanner instance that you want to connect to.\n- \u003cvar translate=\"no\"\u003eSOURCE_FILE_PATH\u003c/var\u003e: the file that contains the SQL that you want to execute.\n- \u003cvar translate=\"no\"\u003eOUTPUT_FILE_PATH\u003c/var\u003e: the named file to append a copy of the SQL output.\n\nStart an interactive session\n----------------------------\n\nYou can start an interactive Spanner CLI session, which lets you directly\ntype SQL statements and meta-commands and see results in the CLI. To do so, run\nthe following command: \n\n gcloud alpha spanner cli \u003cvar translate=\"no\"\u003eDATABASE_ID\u003c/var\u003e --instance=\u003cvar translate=\"no\"\u003eINSTANCE_ID\u003c/var\u003e\n\nUpon successful connection between the CLI and your database, you will see a\nprompt (for example, `spanner-cli\u003e`) where you can do the following:\n\n- Directly type GoogleSQL statements:\n - [GoogleSQL DDL reference](/spanner/docs/reference/standard-sql/data-definition-language)\n - [GoogleSQL DML reference](/spanner/docs/reference/standard-sql/dml-syntax)\n - [GoogleSQL query syntax](/spanner/docs/reference/standard-sql/query-syntax)\n- Execute transactions\n- Use supported [meta-commands](#supported-meta-commands)\n\nAfter pressing the `ENTER` key, the statement or command is sent to the\nappropriate Spanner database. Spanner then\nexecutes the statement or command.\n\nIn the following example, you start an interactive session in `test-database`\nand then execute `SELECT 1;`: \n\n gcloud alpha spanner cli test-database --instance test-instance\n\n Welcome to Spanner-Cli Client.\n Type 'help;' or '\\h' for help.\n Type 'exit;' or 'quit;' or '\\q' to exit.\n\n spanner-cli\u003e SELECT 1;\n +---+\n | |\n +---+\n | 1 |\n +---+\n\n 1 rows in set (1.11 msecs)\n\n### Execute DDL statement\n\nTo execute a DDL statement, you can run the following: \n\n spanner-cli\u003e CREATE TABLE Singers (\n -\u003e SingerId INT64 NOT NULL,\n -\u003e FirstName STRING(1024),\n -\u003e LastName STRING(1024),\n -\u003e SingerInfo STRING(1024),\n -\u003e BirthDate DATE\n -\u003e ) PRIMARY KEY(SingerId);\n\n Query OK, 0 rows affected (17.08 sec)\n\n### Execute DML statement\n\nTo execute a DML statement, you can run the following: \n\n spanner-cli\u003e INSERT INTO Singers (SingerId, FirstName, LastName, SingerInfo)\n -\u003e VALUES(1, 'Marc', 'Richards', 'nationality: USA'),\n -\u003e (2, 'Catalina', 'Smith', 'nationality: Brazil'),\n -\u003e (3, 'Andrew', 'Duneskipper', NULL);\n\n Query OK, 3 rows affected (0.32 sec)\n\nSupported meta-commands\n-----------------------\n\nThe Spanner CLI supports utility meta-commands, which are commands that\noperate on the client, in this case the Spanner CLI. The following\nmeta-commands are supported in the Spanner CLI:\n\nWhat's next\n-----------\n\n- See a list of all supported [`gcloud alpha spanner cli` commands](/sdk/gcloud/reference/alpha/spanner/cli)."]]