Bird
Raised Fist0
GCPcloud~5 mins

Creating a Cloud SQL instance in GCP - Step-by-Step CLI Walkthrough

Choose your learning style10 modes available
Introduction
Creating a Cloud SQL instance lets you have a managed database in the cloud. This means you don't have to worry about setting up or maintaining the database server yourself.
When you want to store app data in a reliable, managed database without managing hardware.
When you need a MySQL or PostgreSQL database that scales automatically with your app.
When you want automatic backups and easy recovery for your database.
When you want to connect your cloud app securely to a database without managing network setup.
When you want to avoid installing and configuring database software on your own servers.
Config File - cloudsql-instance.yaml
cloudsql-instance.yaml
apiVersion: sql.cnrm.cloud.google.com/v1beta1
kind: SQLInstance
metadata:
  name: example-sql-instance
spec:
  databaseVersion: POSTGRES_14
  region: us-central1
  settings:
    tier: db-f1-micro
    backupConfiguration:
      enabled: true
      startTime: 03:00
    ipConfiguration:
      authorizedNetworks:
      - value: 0.0.0.0/0
      ipv4Enabled: true

This YAML file defines a Cloud SQL instance named example-sql-instance.

databaseVersion sets the database type and version (PostgreSQL 14 here).

region is where the instance runs.

settings.tier defines the machine size (small here).

backupConfiguration enables daily backups at 3 AM.

ipConfiguration allows public IP access from anywhere (0.0.0.0/0) and enables IPv4.

Commands
This command creates a Cloud SQL instance named example-sql-instance with PostgreSQL 14, a small machine type, in the us-central1 region. It enables daily backups at 3 AM and allows public IP access from anywhere.
Terminal
gcloud sql instances create example-sql-instance --database-version=POSTGRES_14 --tier=db-f1-micro --region=us-central1 --backup-start-time=03:00 --authorized-networks=0.0.0.0/0
Expected OutputExpected
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/example-sql-instance]. NAME DATABASE_VERSION REGION GCE_ZONE TIER PRIMARY_ADDRESS STATUS example-sql-instance POSTGRES_14 us-central1 us-central1-a db-f1-micro 35.233.123.45 RUNNABLE
→
--database-version - Specifies the database engine and version
→
--tier - Defines the machine size for the instance
→
--authorized-networks - Sets which IP addresses can connect to the instance
This command shows detailed information about the created Cloud SQL instance to verify its settings and status.
Terminal
gcloud sql instances describe example-sql-instance
Expected OutputExpected
name: projects/my-project/instances/example-sql-instance databaseVersion: POSTGRES_14 region: us-central1 settings: tier: db-f1-micro backupConfiguration: enabled: true startTime: 03:00 ipConfiguration: authorizedNetworks: - value: 0.0.0.0/0 ipv4Enabled: true state: RUNNABLE
This command sets the password for the default 'postgres' user on the Cloud SQL instance to secure access.
Terminal
gcloud sql users set-password postgres % --instance=example-sql-instance --password=mysecurepassword123
Expected OutputExpected
Updated user [postgres].
→
--password - Sets the new password for the user
→
--instance - Specifies which Cloud SQL instance to update
Key Concept

If you remember nothing else from this pattern, remember: creating a Cloud SQL instance means setting its database type, machine size, region, and access rules before connecting your app.

Common Mistakes
Not specifying the database version when creating the instance
The command fails or defaults to an unwanted database type/version.
Always include the --database-version flag with the desired database engine and version.
Allowing 0.0.0.0/0 in authorized networks without restricting IPs
This opens the database to the entire internet, risking unauthorized access.
Limit authorized networks to specific IP addresses or ranges you trust.
Not setting a password for the default database user
Leaves the database vulnerable to unauthorized access.
Use gcloud sql users set-password to set a strong password immediately after creating the instance.
Summary
Use gcloud sql instances create with flags to set up a Cloud SQL instance with your desired database version, machine size, and region.
Verify the instance details with gcloud sql instances describe to confirm settings and status.
Secure your database by setting a strong password for the default user using gcloud sql users set-password.

Practice

(1/5)
1. What is the main purpose of creating a Cloud SQL instance in Google Cloud?
easy
A. To store files and objects in the cloud
B. To set up a managed database service without handling server maintenance
C. To create a virtual machine for running applications
D. To configure network firewalls for security

Solution

  1. Step 1: Understand Cloud SQL service

    Cloud SQL is a managed database service that handles database setup and maintenance for you.
  2. Step 2: Identify the purpose of creating an instance

    Creating a Cloud SQL instance means setting up a database server managed by Google Cloud, so you don't manage the server yourself.
  3. Final Answer:

    To set up a managed database service without handling server maintenance -> Option B
  4. Quick Check:

    Cloud SQL = Managed database service [OK]
Hint: Cloud SQL means managed database, not VM or storage [OK]
Common Mistakes:
  • Confusing Cloud SQL with virtual machines
  • Thinking Cloud SQL is for file storage
  • Mixing Cloud SQL with network configuration
2. Which of the following is the correct command to create a Cloud SQL instance named my-instance with MySQL version 8.0 in the region us-central1?
easy
A. gcloud sql instances create my-instance --database-version=MYSQL_8_0 --region=us-central1
B. gcloud sql create instance my-instance --version=MYSQL8 --location=us-central1
C. gcloud sql instances new my-instance --db-version=8.0 --zone=us-central1
D. gcloud sql instances create my-instance --db-version=MYSQL8 --region=us-central1

Solution

  1. Step 1: Recall correct gcloud command syntax

    The correct command starts with gcloud sql instances create followed by the instance name.
  2. Step 2: Check flags for database version and region

    The flag for database version is --database-version and for region is --region. The version value for MySQL 8.0 is MYSQL_8_0.
  3. Final Answer:

    gcloud sql instances create my-instance --database-version=MYSQL_8_0 --region=us-central1 -> Option A
  4. Quick Check:

    Correct flags and syntax = gcloud sql instances create my-instance --database-version=MYSQL_8_0 --region=us-central1 [OK]
Hint: Use --database-version and --region flags exactly [OK]
Common Mistakes:
  • Using incorrect flag names like --version or --db-version
  • Swapping region flag with zone
  • Wrong command order or missing 'instances create'
3. What will be the output of the following command if executed successfully?
gcloud sql instances create test-instance --database-version=POSTGRES_14 --tier=db-f1-micro --region=us-east1
medium
A. An error because tier db-f1-micro is not supported
B. An error because region us-east1 is invalid
C. A new Cloud SQL instance with MySQL 14 in us-east1 region
D. A new Cloud SQL instance named test-instance with PostgreSQL 14 in us-east1 region

Solution

  1. Step 1: Analyze command parameters

    The command creates a Cloud SQL instance named test-instance with PostgreSQL version 14, machine tier db-f1-micro, in region us-east1.
  2. Step 2: Validate parameters

    db-f1-micro is a valid machine tier for small instances, us-east1 is a valid region, and POSTGRES_14 is a valid database version.
  3. Final Answer:

    A new Cloud SQL instance named test-instance with PostgreSQL 14 in us-east1 region -> Option D
  4. Quick Check:

    Valid flags and values create instance = A new Cloud SQL instance named test-instance with PostgreSQL 14 in us-east1 region [OK]
Hint: Check database version and tier validity carefully [OK]
Common Mistakes:
  • Confusing PostgreSQL with MySQL versions
  • Assuming db-f1-micro is invalid tier
  • Mistaking region names
4. You tried to create a Cloud SQL instance with this command:
gcloud sql instances create mydb --database-version=MYSQL_5_7 --tier=n1-standard-1 --region=us-west1

But you get an error saying the tier is invalid. What is the likely cause?
medium
A. The tier name is correct but the database version is invalid
B. The region us-west1 does not support MySQL 5.7
C. The tier name should be db-n1-standard-1 with 'db-' prefix
D. The tier name requires a different format for MySQL

Solution

  1. Step 1: Check tier naming conventions

    Cloud SQL machine tiers use the 'db-' prefix. The correct tier name is 'db-n1-standard-1'.
  2. Step 2: Validate other parameters

    MySQL 5.7 is valid, and us-west1 supports it. The error is due to missing 'db-' prefix in tier name.
  3. Final Answer:

    The tier name should be db-n1-standard-1 with 'db-' prefix -> Option C
  4. Quick Check:

    Tier names include 'db-' prefix [OK]
Hint: Add 'db-' prefix to tier names for Cloud SQL [OK]
Common Mistakes:
  • Omitting 'db-' prefix in tier names
  • Assuming region limits cause tier errors
  • Mixing database version errors with tier errors
5. You want to create a highly available Cloud SQL instance with PostgreSQL 13 in region us-central1. Which command correctly sets up a primary instance with high availability enabled?
hard
A. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --availability-type=REGIONAL
B. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --high-availability
C. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --availability=HIGH
D. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --replica-type=PRIMARY

Solution

  1. Step 1: Identify flag for high availability

    The correct flag to enable high availability in Cloud SQL is --availability-type=REGIONAL.
  2. Step 2: Confirm other parameters

    Database version POSTGRES_13 and region us-central1 are valid. The command uses correct syntax for creating a highly available instance.
  3. Final Answer:

    gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --availability-type=REGIONAL -> Option A
  4. Quick Check:

    Use --availability-type=REGIONAL for HA [OK]
Hint: Use --availability-type=REGIONAL to enable high availability [OK]
Common Mistakes:
  • Using non-existent flags like --high-availability
  • Confusing replica-type with availability
  • Using incorrect availability flag names
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard