In this post, we will take a look at how we can use Google Cloud Platform (GCP) SQL as a database for our Spring Boot application. We will investigate how we can use the Cloud database from our development machine and how we can use it from GCP itself.

1. Create a GCP Account

First, you will need to create a GCP account. Check out the first paragraph of a previous post how to do so.

2. Create the Example Application

We will create a Spring Boot MVC web application which uses a PostgreSQL database in the Cloud. Go to and create a Java 11 Web MVC project. The application will simply return a list of Shows when accessing a shows URL. The sources are available at GitHub.

The only domain object is Show, which has an id and a title. The getters and setters are left out for brevity.

public class Show {
    private Long id;
    private String title;

For accessing the database, we will use JdbcTemplate. We only define a findAll method in the ShowDao which retrieves all Shows from the database. We use a ShowRowMapper in order to map the table columns to the Show domain object.

public class ShowDaoImpl implements ShowDao {

    JdbcTemplate jdbcTemplate;

    public List findAll() {
        return jdbcTemplate.query("select * from show", new ShowRowMapper());


class ShowRowMapper implements RowMapper {
    public Show mapRow(ResultSet rs, int rowNum) throws SQLException {
        Show show = new Show();


        return show;

The ShowService will retrieve the data from the database via the ShowDao object.

public class ShowServiceImpl implements ShowService {

    ShowDao showDao;

    public List findAll() {
        return showDao.findAll();


And finally, we define a shows URL which routes to the shows method from the ShowController and which will return the values from the database as JSON.

public class ShowController {

    ShowService showService;

    public List shows() {
        return showService.findAll();

In file src/main/resources/schema.sql we define the Show table:

    title VARCHAR(200) NOT NULL,
    PRIMARY KEY (id)

In file src/main/resources/data.sql we define some contents for the Show table:

INSERT INTO show VALUES (1, 'My Developer Planet Show 1');
INSERT INTO show VALUES (2, 'My Developer Planet Show 2');
INSERT INTO show VALUES (3, 'My Developer Planet Show 3');
INSERT INTO show VALUES (4, 'My Developer Planet Show 4');
INSERT INTO show VALUES (5, 'My Developer Planet Show 5');

In the pom.xml file, we need a dependency for Spring Web MVC and a dependency for Spring Cloud GCP PostgreSQL.


3. Use GCP SQL from Your Local Development Machine

Now that we have created our application, it is time to set up the database and the necessary configuration in our application in order to run it from our local development machine.

3.1 Create Instance

In the GCP console, go to SQL and create a SQL instance.

create sql instance

We have the choice between MySQL and PostgreSQL. We choose PostgreSQL.

create PostgreSQL instance

Fill in the necessary configuration options. We set the Instance ID to myspringcloud, set up a password for the postgres user, choose a region and click the Create button.

create PostgreSQL isntance configuration

After a minute or so, the SQL instance is created.

instance created

3.2 Create Database

Click the Instance ID myspringcloud and select the Databases tab.

create database 1

Click the Create database button and create a myspringclouddb database.

create database 2

3.3 Allow Connection from Local Machine

By default, it is not allowed to connect to the database from outside your GCP account. We therefore need to allow the IP address from our local development machine. Go to the Instance Details and the Connections tab. Ensure that Public IP is enabled.

instance connection public 1

Click the Add network button, fill in a name for your connection, e.g. mydevelopermachine and fill in your public IP address which can be retrieved via At last, click the Done button and the Save button.

instance connection public 2

3.4 Create Service Account

Last thing to do is to create a service account for our local development machine. Go to IAM & admin - Service accounts and click the Create Service Account button.

iam - create service account 1

Enter a name for your service account, in our case mydevelopermachine and click the Create button.

iam - create service account 2

Grant the service account the Owner role and click the Continue button.

iam - create service account 3

In the last step, we click the Create key button.

iam - create service account 4

Choose JSON as Key type and click the Create button.

iam - create service account 5

A JSON file is downloaded, keep it safe somewhere, we will need it later on.

3.5 Spring Database Configuration

Now that we have set up the database, it is time to return to the Spring application. We will need to configure some properties in the file.


Let’s explain the different properties:

spring.datasource.username: this is the database user we will use. It is also possible of course to create a user specifically for your application, but for simplicity, we use the postgres user for it.

spring.datasource.password: the password of the postgres user, replace [password] with your own password. the GCP Cloud database name which we defined previously. the connection name of the GCP SQL instance. This must be of the form [gcp-project-id]:[region]:[instance-name]

spring.datasource.continue-on-error: we will create the tables on startup, this property will prevent failure when the table already exists.

spring.datasource.initialization-mode: this property will create the table and fill the table with data during startup.

spring.datasource.schema: we indicate where the schema for our database can be found. we indicate where the data for our database can be found. this property is necessary for accessing the Cloud database from our local development machine and must contain the GCP project ID. this property is necessary for accessing the Cloud database from our local development machine and must refer to the JSON file we downloaded for our service account.

3.6 Run the Application

Run the Spring Boot application from our local development machine.

$ mvn spring-boot:run

Go to URL http://localhost:8080/shows when the application has started successfully. The following response is returned.

  {"id":1,"title":"My Developer Planet Show 1"},
  {"id":2,"title":"My Developer Planet Show 2"},
  {"id":3,"title":"My Developer Planet Show 3"},
  {"id":4,"title":"My Developer Planet Show 4"},
  {"id":5,"title":"My Developer Planet Show 5"}

4. Use GCP SQL from the GCP Console

In this paragraph, we will adapt our previously created application and configuration in order to run it from the GCP console. The sources can be found in branch feature/gcpconsole available at GitHub.

We need to adapt the Java version in our pom.xml file (Java 1.8 is the default on GCP console):


In the file, we remove the entries which were necessary for access from our local development machine: and

We also don’t need the service account mydevelopermachine and it is not necessary to enable the Public IP connection.

Go to the GCP console and clone the git repository:

$ git clone --branch feature/gcpconsole

Enter the myspringcloudgcpplanet directory and run the application:

$ mvn spring-boot:run

Start the Web Preview which will open a browser tab to the following URL:

This wil return a 404 error. Adapt the URL in order to retrieve the shows:

The response is identical to the one issued from our local machine.

5. Conclusion

In this post, we created a Spring Boot MVC web application with a Cloud PostgreSQL database. We ran the application from our local development machine and from within the GCP console. It was fairly easy to use the GCP SQL database. In only a few minutes you have an up-and-running database in the Cloud which can be accessed from a local machine or from within your GCP account. Don’t forget to remove the database instances after experimenting!