0
0
GCPcloud~5 mins

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

Choose your learning style9 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.