Integrate Spanner with Spring Data JPA (PostgreSQL dialect)
Stay organized with collections
Save and categorize content based on your preferences.
Spring Data JPA, part of the larger Spring Data family, makes it easier to
implement JPA based repositories. Spring Data JPA supports PostgreSQL
and a wide range of other database systems. It adds an abstraction layer between
your application and your database that makes your application easier to port
from one database system to another.
Set up Spring Data JPA for Spanner PostgreSQL-dialect databases
You can integrate Spanner PostgreSQL-dialect databases with Spring Data JPA
using the standard PostgreSQL Hibernate dialect and
PGAdapter.
Add the following method to your application to start PGAdapter
directly from your Java application. PGAdapter runs in the same JVM as
your application, and the application connects to PGAdapter on
localhost:port.
/** Starts PGAdapter in-process and returns a reference to the server. */staticProxyServerstartPGAdapter(){// Start PGAdapter using the default credentials of the runtime environment on port 9432.OptionsMetadataoptions=OptionsMetadata.newBuilder().setPort(9432).build();ProxyServerserver=newProxyServer(options);server.startServer();server.awaitRunning();returnserver;}
Configuration
Configure application.properties to use the PostgreSQL Hibernate
Dialect and the PostgreSQL JDBC Driver. Configure the PostgreSQL
JDBC Driver to connect to a PostgreSQL-dialect database through PGAdapter.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Integrate Spanner with Spring Data JPA (PostgreSQL dialect)\n\nSpring Data JPA, part of the larger Spring Data family, makes it easier to\nimplement JPA based repositories. Spring Data JPA supports PostgreSQL\nand a wide range of other database systems. It adds an abstraction layer between\nyour application and your database that makes your application easier to port\nfrom one database system to another.\n\nSet up Spring Data JPA for Spanner PostgreSQL-dialect databases\n---------------------------------------------------------------\n\nYou can integrate Spanner PostgreSQL-dialect databases with Spring Data JPA\nusing the standard PostgreSQL Hibernate dialect and\n[PGAdapter](/spanner/docs/pgadapter).\n\nTo see an example, refer to the full\n[working sample application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/java/spring-data-jpa) on\nGitHub.\n\n### Dependencies\n\nIn your project, add Apache Maven dependencies for [Spring Data JPA](https://spring.io/projects/spring-data-jpa),\nthe [PostgreSQL JDBC driver](https://github.com/pgjdbc/pgjdbc), and [PGAdapter](/spanner/docs/pgadapter). \n\n \u003cdependencies\u003e\n \u003c!-- Spring Data JPA --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n \u003cartifactId\u003espring-boot-starter-data-jpa\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c!-- Add the PostgreSQL JDBC driver --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.postgresql\u003c/groupId\u003e\n \u003cartifactId\u003epostgresql\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c!-- Add PGAdapter as a dependency, so we can start it in-process --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.cloud\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-cloud-spanner-pgadapter\u003c/artifactId\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n\n### Start PGAdapter in-process\n\nAdd the following method to your application to start PGAdapter\ndirectly from your Java application. PGAdapter runs in the same JVM as\nyour application, and the application connects to PGAdapter on\n`localhost:port`. \n\n /** Starts PGAdapter in-process and returns a reference to the server. */\n static ProxyServer startPGAdapter() {\n // Start PGAdapter using the default credentials of the runtime environment on port 9432.\n OptionsMetadata options = OptionsMetadata.newBuilder().setPort(9432).build();\n ProxyServer server = new ProxyServer(options);\n server.startServer();\n server.awaitRunning();\n\n return server;\n }\n\n### Configuration\n\nConfigure `application.properties` to use the PostgreSQL Hibernate\nDialect and the PostgreSQL JDBC Driver. Configure the PostgreSQL\nJDBC Driver to connect to a PostgreSQL-dialect database through PGAdapter. \n\n # The example uses the standard PostgreSQL Hibernate dialect.\n spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect\n\n # Defining these properties here makes it a bit easier to build the connection string.\n # Change these to match your Cloud Spanner PostgreSQL-dialect database.\n spanner.project=my-project\n spanner.instance=my-instance\n spanner.database=my-database\n # This setting ensures that PGAdapter automatically commits the current transaction if it encounters\n # a DDL statement in a read/write transaction, and then executes the DDL statements as a single DDL\n # batch. \n spanner.ddl_transaction_mode=options=-c%20spanner.ddl_transaction_mode=AutocommitExplicitTransaction\n\n # This is the connection string to PGAdapter running in-process.\n spring.datasource.url=jdbc:postgresql://localhost:9432/projects%2F${spanner.project}%2Finstances%2F${spanner.instance}%2Fdatabases%2F${spanner.database}?${spanner.ddl_transaction_mode}\n\n # You can display SQL statements and stats for debugging if needed.\n spring.jpa.properties.hibernate.show_sql=true\n spring.jpa.properties.hibernate.format_sql=true\n\n # Enable JDBC batching.\n spring.jpa.properties.hibernate.jdbc.batch_size=100\n spring.jpa.properties.hibernate.order_inserts=true\n\nFull Sample Application\n-----------------------\n\nA [working sample application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/java/spring-data-jpa) is\navailable on GitHub.\n\nWhat's next\n-----------\n\n- Learn more about [Spring Data JPA](https://spring.io/projects/spring-data-jpa).\n- Learn more about [Hibernate ORM](https://hibernate.org/orm/).\n- View the repository for [PGAdapter](https://github.com/GoogleCloudPlatform/pgadapter) on GitHub.\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/pgadapter/issues) to report a bug or ask a question about PGAdapter.\n- [Integrate Spanner with Spring Data JPA (GoogleSQL dialect)](/spanner/docs/use-spring-data-jpa)."]]