codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

61 / 67 topics
59Cloud Databases60AWS RDS61Google Cloud SQL62Azure SQL Database
Tutorials/SQL & Databases/Google Cloud SQL
🗄️SQL & Databases

Google Cloud SQL

Updated 2026-04-20
5 min read

Introduction to Google Cloud SQL

Google Cloud SQL is a fully-managed database service that helps you set up, maintain, manage, and scale your relational databases on Google Cloud Platform (GCP). It supports popular open-source relational databases like MySQL, PostgreSQL, and SQL Server. This tutorial will guide you through setting up and using Google Cloud SQL for your applications.

Prerequisites

Before diving into the setup and usage of Google Cloud SQL, ensure that you have:

  • A Google Cloud account.
  • The Google Cloud SDK installed on your local machine.
  • Basic knowledge of SQL and database management.

Setting Up Google Cloud SQL

1. Create a New Project

First, create a new project in the Google Cloud Console if you don't already have one.

  1. Go to the Google Cloud Console.
  2. Click on the project dropdown at the top of the page and select "New Project."
  3. Enter a name for your project and click "Create."

2. Enable Billing

Ensure that billing is enabled for your project, as Google Cloud SQL requires it.

  1. Navigate to the "Billing" section in the Google Cloud Console.
  2. Click on "Enable billing" and follow the prompts to link your payment method.

3. Enable the Cloud SQL API

The Cloud SQL API must be enabled for your project.

  1. Go to the "APIs & Services" > "Library" in the Google Cloud Console.
  2. Search for "Cloud SQL Admin API."
  3. Click on it and then click "Enable."

Creating a Cloud SQL Instance

1. Create an Instance

You can create a new Cloud SQL instance using the Google Cloud Console or the gcloud command-line tool.

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Click on "Create Instance."
  3. Choose your database engine (MySQL, PostgreSQL, or SQL Server).
  4. Configure the instance settings:
    • Instance ID: A unique identifier for your instance.
    • Region and Zone: Select a region and zone where you want to deploy your instance.
    • Machine Type: Choose a machine type based on your performance needs.
  5. Click "Create" to launch the instance.

Using gcloud Command-Line Tool

gcloud sql instances create my-instance --database-version=MYSQL_8_0 \
    --cpu=2 --memory=4GB --region=us-central1 --zone=us-central1-a

2. Configure Networking

After creating the instance, configure the networking settings to allow connections.

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Select your instance and click on "Connections."
  3. Under "Authorized networks," add a new network by entering the IP address or range that you want to allow access from.
  4. Save the changes.

Connecting to Your Cloud SQL Instance

1. Using the gcloud Command-Line Tool

You can connect to your Cloud SQL instance using the gcloud sql connect command.

gcloud sql connect my-instance --user=root

2. Using a Database Client

You can also use third-party database clients like MySQL Workbench, pgAdmin, or DBeaver to connect to your Cloud SQL instance.

  1. Open your preferred client.
  2. Enter the connection details:
    • Host: The public IP address of your Cloud SQL instance.
    • Port: Default is 3306 for MySQL, 5432 for PostgreSQL, and 1433 for SQL Server.
    • User: Your database user (e.g., root).
    • Password: Your database password.

Managing Databases

1. Creating a Database

You can create a new database using SQL commands or the Google Cloud Console.

Using SQL Commands

CREATE DATABASE mydatabase;

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Select your instance and click on "Databases."
  3. Click "Create Database" and enter a name for your database.
  4. Save the changes.

2. Managing Users

You can create, modify, or delete users using SQL commands.

Creating a User

CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword';

Granting Privileges

GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'%';
FLUSH PRIVILEGES;

Backup and Restore

1. Creating Backups

Google Cloud SQL automatically creates daily backups of your instances. You can also create on-demand backups.

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Select your instance and click on "Backups."
  3. Click "Create Backup" to create an on-demand backup.

2. Restoring from a Backup

You can restore your database from a previous backup using the Google Cloud Console or gcloud command-line tool.

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Select your instance and click on "Backups."
  3. Click on the backup you want to restore.
  4. Click "Restore Instance" and follow the prompts.

Scaling Your Cloud SQL Instance

1. Vertical Scaling

Vertical scaling involves increasing the resources (CPU, memory) of your existing instance.

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Select your instance and click on "Settings."
  3. Under "Machine type," select a new machine type with higher CPU or memory.
  4. Save the changes.

2. Horizontal Scaling

Horizontal scaling involves adding more instances to distribute the load.

Using the Google Cloud Console

  1. Go to the "SQL" section in the Google Cloud Console.
  2. Click on "Create Instance."
  3. Configure a new instance with similar settings as your existing one.
  4. Use a load balancer or application logic to distribute traffic between instances.

Best Practices

  • Regular Backups: Ensure that regular backups are enabled and tested.
  • Network Security: Restrict access to your Cloud SQL instance using authorized networks.
  • Monitoring and Logging: Use Google Cloud's monitoring and logging tools to keep track of your database performance.
  • Resource Optimization: Optimize CPU and memory settings based on your workload.

Conclusion

Google Cloud SQL provides a robust, scalable, and managed solution for running relational databases in the cloud. By following this tutorial, you should be able to set up, manage, and scale your Cloud SQL instances effectively. Remember to regularly review and optimize your database configurations to ensure optimal performance and security.


PreviousAWS RDSNext Azure SQL Database

Recommended Gear

AWS RDSAzure SQL Database