skip to Main Content

I created Postgress + Spring app and It works perfectly locally (I had different docker-compose file fot it, but it works)

The target is: I want to deploy it to GCP.

I’m already running Postgress DB on GCP.
Done more or less with this tutorial

And tbh for me it looks good:

Now I want also put my api onto the GCP that will be using this DB
But firstly I need to test it locally.
I’m running my app with docker:
enter image description here

And when I try to run it connection fails:
enter image description here

What am I missing?

It’s my first time deploying DB + API onto GCP, so I’m kinda doing it blindly
So any reamrks will be apprecieated 😀

Sorry for so little information, but I honestly don’t know what more info I can give You :/

(I do not care wheter it is done perfectly or not)

2

Answers


  1. To connect to a PostgreSQL instance on Cloud SQL I’d recommend that you read over the documentation because there are multiple ways of doing it.

    Basically the connection requires:

    • Having a client to connect from.
    • Configuring the db to accept connections and allow the client’s requests. This includes not only granting a public IP but also whitelisting the given client’s source IP address/range.
    • Performing the connection itself.

    In your particular case, my recommendation would be that you run locally a Cloud SQL Auth Proxy container along your application. It allows you to perform the connections by simply using the connection name and the credentials of a Service Account with client access (at least roles/cloudsql.client). Using this proxy also eliminates the need of manually whitelisting your source IP.

    version: '3'
    services:
      cloudsql-proxy:
        image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.7.2
        command: ${INSTANCE_CONNECTION_NAME} --credentials-file=/credentials.json --address 0.0.0.0 --port 5432
        volumes:
        - ./sqlproxy_key.json:/credentials.json
        ports:
        - 5432:5432
      app:
        [...]
        depends_on:
        - cloudsql-proxy
    

    It will also allow you to perform connections from both your local computer and whichever GCP service you choose rather similarly (depending on the service). As a best practice, the idea would be to connect to one host or another according to the environment variables read from your application.

    I’d recommend that you go through this document to understand how the connection is configured with the Cloud SQL Auth Proxy, and this document to see how it’s done from Cloud Run (as an example).

    Login or Signup to reply.
  2. You may try to check this link wherein in it uses JSON environment file.

    services:
      app:
        environment:
          SPRING_APPLICATION_JSON: '{
            "spring.datasource.url"  : "jdbc:postgresql://postgresdb:$POSTGRESDB_DOCKER_PORT/$POSTGRESDB_DATABASE",
            "spring.datasource.username" : "$POSTGRESDB_USER",
            "spring.datasource.password" : "$POSTGRESDB_ROOT_PASSWORD",
            "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.PostgreSQLDialect",
            "spring.jpa.hibernate.ddl-auto" : "update"
          }'
        volumes:
          - .m2:/root/.m2
        stdin_open: true
        tty: true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search