Client libraries and sample code for Cloud SQL

MySQL  |  PostgreSQL  |  SQL Server

The Cloud SQL Admin API is built on HTTP and JSON, so any standard HTTP client cansend requests to it and parse the responses. However, instead of creating HTTPrequests and parsing responses manually, you may want to use the Google APIsclient libraries. The client libraries provide better language integration,improved security, and support for making calls that require user authorization.

Client libraries can useApplication Default Credentials to easily authenticate with Google APIs and send requests to those APIs. With Application Default Credentials, you can test your application locally and deploy it without changing the underlying code. For more information, see Authenticate for using client libraries.

Application Default Credentials are part of the client libraries you can use toaccess Cloud SQL. Default credentials identify your application with either auser credential or a default service account. When working with a library, it'stypical to choose credentials based on the type of environment whereyou run code. For example, in a development environment, you can authenticatewith thegcloud auth command and the client libraries will use thosecredentials. For more information about environments, seeAuthentication overview in theGoogle Cloud Platform Auth Guide.

Access the service

Depending on the Cloud SQL Admin API client library you use, you may need toconfigure how the library discovers the default service path. For clientlibraries that use the Google APIs Discovery Service, use the API namesqladmin to build a client. This includes libraries for Python and JavaScript.

The following code snippets show how to create a client and listCloud SQL instances in a project.

C++

For theC++ Client Library, followSetting up a C++ development environment to install the library.

#include"google/cloud/sql/v1/sql_instances_client.h"#include"google/cloud/project.h"#include <iostream>intmain(intargc,char*argv[])try{if(argc!=2){std::cerr <<"Usage: " <<argv[0] <<" project-id\n";return1;}namespacesql=::google::cloud::sql_v1;autoclient=sql::SqlInstancesServiceClient(sql::MakeSqlInstancesServiceConnectionRest());google::cloud::sql::v1::SqlInstancesListRequestrequest;request.set_project(argv[1]);for(autodatabase:client.List(request)){if(!database)throwstd::move(database).status();std::cout <<database->DebugString() <<"\n";}return0;}catch(google::cloud::Statusconst&status){std::cerr <<"google::cloud::Status thrown: " <<status <<"\n";return1;}

Java

For theClient Library for Java, you can optionally specify the service path directly.

// Set up global SQLAdmin instance.client = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)    .setServicePath("sql/v1beta4/")    .setApplicationName(APPLICATION_NAME).build();InstancesListResponse resp = client.instances().list("PROJECT_ID").execute();List<DatabaseInstance> list = resp.getItems();for (DatabaseInstance d : list) {    System.out.println(d.getName());}

Go

For theClient Library for Go, import thesqladmin package.

funcListInstances(projectIdstring)([]*sqladmin.DatabaseInstance,error){ctx:=context.Background()// Create an http.Client that uses Application Default Credentials.hc,err:=google.DefaultClient(ctx,sqladmin.SqlserviceAdminScope)iferr!=nil{returnnil,err}// Create the Google Cloud SQL service.service,err:=sqladmin.New(hc)iferr!=nil{returnnil,err}// List instances for the project ID.instances,err:=service.Instances.List(projectId).Do()iferr!=nil{returnnil,err}returninstances.Items,nil}

Java

For theClient Library for Java, you can optionally specify the service path directly.

// Set up global SQLAdmin instance.client = new SQLAdmin.Builder(httpTransport, JSON_FACTORY, credential)    .setApplicationName(APPLICATION_NAME).build();InstancesListResponse resp = client.instances().list("PROJECT_ID").execute();List<DatabaseInstance> list = resp.getItems();for (DatabaseInstance d : list) {    System.out.println(d.getName());}

JavaScript

For theClient Library for JavaScript, specifysqladmin to build a client.

gapi.client.load('sqladmin', 'v1beta4', function() { console.log('loaded');});gapi.client.sql.instances.list({'project': PROJECT_ID}).execute(showResult);function showResult(result) {  // Process the result.};

Python

For theClient Library for Python, specifysqladmin to build a client.

from googleapiclient import discovery# Construct the service object for the interacting with the Cloud SQL Admin API.service = discovery.build('sqladmin', 'v1beta4', http=http)req = service.instances().list(project="PROJECT_ID")resp = req.execute()print(json.dumps(resp, indent=2))

Theservice object queries the discovery document and uses the correct service path, in this case, "sql/v1beta4/projects/".

If you are looking for code samples that show how applications can connect toCloud SQL, see theConnecting overview page.

Libraries and sample code

LanguageDocumentation
C++Google API Client Library
DartGoogle API Client Library
GoGoogle API Client Library
Sample code: Cloud SQL Auth Proxy
JavaGoogle API Client Library
Google Client Developer's Guide
Sample code: Cloud SQL Java Connector
JavaScriptGoogle API Client Library
.NETGoogle API Client Library
Google Client Developer's Guide
Node.jsGoogle API Client Library
Objective-CGoogle API Client Library
PHPGoogle API Client Library
PythonGoogle API Client Library
Google Client Developer's Guide
Cloud SQL Python Connector
Sample code: MySQL, PostgreSQL, SQL Server
RubyGoogle API Client Library

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-16 UTC.