Skip to content

Cloud SQL

Introduction to SQL for BigQuery and Cloud SQL

There's a Query Editor that accepts SQL statements.

SELECT start_station_name FROM `bigquery-public-data.london_bicycles.cycle_hire` GROUP BY start_station_name;

AS

SELECT start_station_name, COUNT(*) AS num_starts FROM `bigquery-public-data.london_bicycles.cycle_hire` GROUP BY start_station_name;

The most popular starting point is Belgrove Street, King's Cross:

SELECT start_station_name, COUNT(*) AS num_starts FROM `bigquery-public-data.london_bicycles.cycle_hire` GROUP BY start_station_name ORDER BY num_starts DESC;

Importing CSV Files

There are two formats of data accepted by Cloud SQL: dump files (.sql) or CSV files (.csv).

Store them in a bucket. Import the CSV into a table in a database.

UNION

This keyword combines the output of two or more SELECT queries into a result-set.

SELECT start_station_name AS top_stations, num FROM london1 WHERE num>100000
UNION
SELECT end_station_name, num FROM london2 WHERE num>100000
ORDER BY top_stations DESC;
The results help to clarify what this query is doing:

+-------------------------------------+--------+
| top_stations                        | num    |
+-------------------------------------+--------+
| Wormwood Street, Liverpool Street   | 119447 |
| Wormwood Street, Liverpool Street   | 129376 |
| Wellington Arch, Hyde Park          | 110260 |
| Wellington Arch, Hyde Park          | 105729 |
| Waterloo Station 3, Waterloo        | 201630 |
| Waterloo Station 3, Waterloo        | 193200 |
| Waterloo Station 1, Waterloo        | 145910 |
| Waterloo Station 1, Waterloo        | 141733 |

Cloud SQL for MySQL: Qwik Start

Cloud SQL for PostgreSQL: Qwik Start

A sample project using SQLAlchecmy to connect to Cloud SQL.

Interesting bits include:

def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
# keep secrets safe.

db_user = os.environ["DB_USER"] # e.g. 'my-database-user'
db_pass = os.environ["DB_PASS"] # e.g. 'my-database-password'
db_name = os.environ["DB_NAME"] # e.g. 'my-database'
unix_socket_path = os.environ["INSTANCE_UNIX_SOCKET"] # e.g. '/cloudsql/project:region:instance'

A few more general notes are on the Google Cloud page. Manage PostgreSQL Databases on Cloud SQL