0

I've been trying to connect my spring boot service to Google cloud Postgres instance but facing an issue. I'm migrating a service from golang, that's using a connector to communicate with the database, to spring boot service. I am following this tutorial provided by GCP github account

pom

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>4.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
    ... 
        <!-- Database Driver -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
        </dependency>

application.properties

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.sql.init.mode=always
# GCP settings
gcp.project=project_id
gcp.project.region=project_region
gcp.postgres.instance=postgres_instance_id
gcp.postgres.database=database_name
# PostgreSQL connection
spring.cloud.gcp.project-id=${gcp.project}
spring.cloud.gcp.sql.instance-connection-name=${gcp.project}:${gcp.project.region}:${gcp.postgres.instance}
spring.cloud.gcp.sql.database-name=${gcp.postgres.database}

spring.datasource.url=jdbc:postgresql:///${gcp.postgres.database}?cloudSqlInstance=${gcp.project}:${gcp.project.region}:${gcp.postgres.instance}&socketFactory=com.google.cloud.sql.postgres.SocketFactory
spring.datasource.username=db_user
spring.datasource.password=db_pass

When I run my spring boot app from Intellij I always get:

2025-11-13T15:03:09.275+01:00  WARN 36220 --- [management] [           main] c.g.a.oauth2.DefaultCredentialsProvider  : Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.

2025-11-13T15:04:09.460+01:00  WARN 36220 --- [management] [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Pool is empty, failed to create/setup connection (3ddfd9fb-cb9a-4533-9dfe-3fd0ded3b497)

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:385) ~[postgresql-42.7.8.jar:42.7.8]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:57) ~[postgresql-42.7.8.jar:42.7.8]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:279) ~[postgresql-42.7.8.jar:42.7.8]
    at org.postgresql.Driver.makeConnection(Driver.java:448) ~[postgresql-42.7.8.jar:42.7.8]
    at org.postgresql.Driver.connect(Driver.java:298) ~[postgresql-42.7.8.jar:42.7.8]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:144) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:370) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:207) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:488) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:576) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:97) ~[HikariCP-6.3.3.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:111) ~[HikariCP-6.3.3.jar:na]
... 

Caused by: java.net.SocketException: Connection reset

What am I doing wrong?

6
  • according to the logs you posted, you don't have actual errors, only warnings. Did you read the warnings you got? Commented Nov 13 at 14:23
  • 1
    Please don't use quotes to format a stacktrace, use code tags that way it remains readable. Judging from what I can decipher is you get a connection reset, which generally is a routing/fw issues. Commented Nov 13 at 14:24
  • Use code format for logs, not quote format. Quote is for human text, not logs and other pre formatted text. Quote breaks the existing format of the log, makes it very hard to read and gets in the way of trying to copy text for investigation. Commented Nov 13 at 15:06
  • @M.Deinum done. PSQLException the connection attempt failed. I am confused because the same settings work for the golang service. Commented Nov 13 at 15:26
  • do you have the same service composition as it was for Go? i mean: you need to have VPC connection to sql (if it is a cloudrun) or may be your SQL instance have restrictions on subnet/ip level. you need to clear isolated connection test from the node where you run your Spring app to SQL connection with proper creds docs.cloud.google.com/sql/docs/debugging-connectivity Commented Nov 14 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.