arrow_back

Creating Spanner Instances and Databases (Console)

Join Sign in
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Creating Spanner Instances and Databases (Console)

Lab 1 hour universal_currency_alt 1 Credit show_chart Introductory
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

SCBL001

Overview

In this lab, you create Spanner instances and databases using the Google Cloud Console.

Objectives

In this lab, you learn how to:

  • Create a simple Spanner instance.
  • Compare Spanner configuration options.
  • Create Spanner databases using the Console.
  • Create Spanner databases using the PostgreSQL dialect.

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Create a Simple Spanner instance

  1. On the Google Cloud Console title bar, click the Navigation Menu (nav menu icon). Scroll to the Database section and click Spanner.

  2. Click the Create a Provisioned Instance button and name your instance my-first-instance. Select the region from the Choose a configuration dropdown and then click Create.

  3. Wait for the instance to be created, then you should be redirected to the Instance Overview page. Click the Create Database button, you may need to scroll to the bottom to find it. Name your database my-database, choose Google Standard SQL, then scroll down and click the Create button.

  4. Click Spanner Studio in the left menu listing.

  5. Click the blue + icon on the top ribbon.

  6. Enter the following CREATE TABLE statement, and then click the Run button.

CREATE TABLE MyTable ( TableID INT64 NOT NULL, Field1 STRING(MAX) NOT NULL ) PRIMARY KEY (TableID);
  1. Wait for the operation to complete. Your table should appear in the list of tables for that database. Expand the Tables section of the Explorer menu on the left to see the table name.

  2. Click Clear Query and run the following INSERT query to add a row the your new table. Assuming you have no syntax errors, it should tell you 1 row was inserted. In the query, you are simply inserting a record.

INSERT INTO MyTable (TableID, Field1) VALUES (1, "Here is some data");
  1. Repeat the previous steps to add a few more records. Don't forget to change the TableID field each time so it is unique. You can enter any values you like for Field1. Add 2 or 3 more records.

  2. Click Clear Query and run the following SELECT query.

SELECT * FROM MyTable;
  1. Examine the results in the Console.

Query Results

Task 2. Examining Spanner configuration options

  1. In the top-left corner of the Spanner workspace area, click the All instances link.

  2. Click the Create Instance button and name your instance my-second-instance.

  3. In the Choose a configuration section, click the Compare Region Configurations button.

    In the resulting pane, you must select two configurations to compare. In the first dropdown, select the Regional configuration us-central1 (Iowa). In the second dropdown, select Multi-region configuration nam3 (Northern Virginia/South Carolina/Iowa).
    Take note of the differences between replicas, availability, and the costs.
    Change the first dropdown to the Multi-region configuration nam-eur-asia1 (Iowa/Oklahoma/Belgium/Taiwan/South Carolina) and compare the replicas and the costs between the two multi-region configuration.
  4. Close the Compare region configurations pane. Return to the configuration screen and set the configuration to Regional and .

  5. In the Allocate compute capacity section, change the Unit dropdown to Nodes.

  6. In the Summary section, note the costs and maximum storage capacity. Increase the number of nodes to see the changes to costs and capacity.

  7. In the Allocate compute capacity section, change the Unit dropdown to Processing Units. Processing units are added in increments of 100. Set the value to 100 (this is the smallest Spanner instance possible).

  8. Note the costs and capacity. Click Create to create the instance.

Task 3. Creating the Pets database

  1. As you did earlier, create a database called pets-db in the second instance.

  2. Add the tables Owners and Pets to the database using the following DDL code in the Spanner Studio.

CREATE TABLE Owners ( OwnerID STRING(36) NOT NULL, OwnerName STRING(MAX) NOT NULL ) PRIMARY KEY (OwnerID); CREATE TABLE Pets ( PetID STRING(36) NOT NULL, OwnerID STRING(36) NOT NULL, PetType STRING(MAX) NOT NULL, PetName STRING(MAX) NOT NULL, Breed STRING(MAX) NOT NULL, ) PRIMARY KEY (PetID);
  1. Add a few owners and pets to the tables similar to what you did earlier in the lab. If you need some help, the insert queries should be similar to what is shown below.
INSERT INTO Owners (OwnerID, OwnerName) VALUES ("ca9fdb5c-97b5-4030-91f2-5cf5d7573d59", "Doug"); INSERT INTO Pets (PetID, OwnerID, PetType, PetName, Breed) VALUES ("3be181a7-bafb-40f0-8068-7a021ed5794a", "ca9fdb5c-97b5-4030-91f2-5cf5d7573d59", "Dog", "Noir", "Schnoodle");

Note: The primary keys for this database are not integers, but 36 character strings. These keys are meant to be universally unique identifiers (UUIDs). You could enter any unique strings less than or equal to 36 characters and it would work. If you want to generate proper UUIDs though, you can make the following request in a browser and at the top of the page, a UUID will be shown.

https://duckduckgo.com/?q=uuid

Bonus Task 4. Creating a PostgreSQL database

  1. Create a second database in your Spanner instance named my-second-instance. Name the database pets-postgres. This time, choose PostgreSQL when prompted to select a database dialect. As a reminder, make sure you are at the Instance Overview page. Click the Create Database button.

  2. The DDL code for the Pets database using PostgreSQL syntax is as follows. Create these tables in the same manner as earlier steps using the Spanner Studio.

CREATE TABLE Owners ( OwnerID VARCHAR(36) PRIMARY KEY, OwnerName VARCHAR(1024) NOT NULL ); CREATE TABLE Pets ( PetID VARCHAR(36) NOT NULL, OwnerID VARCHAR(36) NOT NULL, PetType VARCHAR(1024) NOT NULL, PetName VARCHAR(1024) NOT NULL, Breed VARCHAR(1024) NOT NULL, PRIMARY KEY (PetID) );

Congratulations! You have created Spanner instances and databases using the Google Cloud Console.

End your lab

When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.