Integrate Spanner with Spring Data JDBC (PostgreSQL)

Spring Data JDBC, part of the larger SpringData family, makes it easier to implement JDBC based repositories in yourapplication. Spring Data JDBC supports PostgreSQL andother database systems. It adds an abstraction layer between your applicationand your database that makes your application easier to port from one databasesystem to another.

Set up Spring Data JDBC for Spanner PostgreSQL-dialect databases

You can integrate Spanner PostgreSQL-dialect databases with Spring Data JDBCusing the standard PostgreSQL dialect that is included withSpring Data JDBC and the Spanner JDBC driver.

You don't need to usePGAdapter for this integration.

Dialect configuration

Spring Data JDBC selects the database dialect it uses based onthe JDBC driver that has been configured as a data source in Spring Data.You must add an additional Spring configuration class to your application toinstruct Spring Data JDBC to use the PostgreSQL dialectwhen the Spanner JDBC driver is used:

@ConfigurationpublicclassJdbcConfigurationextendsAbstractJdbcConfiguration{/** Override the dialect auto-detection, so it also returns PostgreSQL for Spanner. */@OverridepublicDialectjdbcDialect(@NonnullNamedParameterJdbcOperationsoperations){if(isCloudSpannerPG(operations.getJdbcOperations())){returnPostgresDialect.INSTANCE;}returnsuper.jdbcDialect(operations);}/** Returns true if the current database is a Spanner PostgreSQL-dialect database. */publicstaticbooleanisCloudSpannerPG(JdbcOperationsoperations){returnBoolean.TRUE.equals(operations.execute((ConnectionCallback<Boolean>)connection->connection.isWrapperFor(CloudSpannerJdbcConnection.class)                        &&com.google.cloud.spanner.Dialect.POSTGRESQL.equals(connection.unwrap(CloudSpannerJdbcConnection.class).getDialect())));}}

To see an example, refer to the fullworking sample application on GitHub.

Dependencies

In your project, add Apache Maven dependencies forSpring Data JDBC and theSpanner JDBC driver.

<dependencies><!--SpringDataJDBC--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><!--SpannerJDBCdriver--><dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-spanner-jdbc</artifactId></dependency><dependencies>

Data source configuration

Configureapplication.properties to use the Spanner JDBC driver andconnect to a Spanner PostgreSQL-dialect database.

# This profile uses a Spanner PostgreSQL database.spanner.project=my-projectspanner.instance=my-instancespanner.database=spring-data-jdbcspring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriverspring.datasource.url=jdbc:cloudspanner:/projects/${spanner.project}/instances/${spanner.instance}/databases/${spanner.database}

Full Sample Application

To try this integration with a sample application, seeSpring Data JDBC Sample Application with SpannerPostgreSQL.

What's next

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-12-17 UTC.