1
votes

I deploy a native quarkus application under cloud run. This application need to connect to a cloud sql postgresql database. On the configuration panel's cloud run, i create a Cloud SQL connections (db-instance-name eq. cloud sql) and some variable as DB_USER, DB_PASSWORD, DB_NAME On Quarkus, i define properties as below :

quarkus.datasource.jdbc.url=jdbc:postgresql:///${DBNAME}:5432
quarkus.datasource.driver=org.postgresql.Driver
quarkus.datasource.username=${DB_USER}
quarkus.datasource.password=${DB_PASSWORD}

My pom.xml

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>   
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>    
<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>postgres-socket-factory</artifactId>
    <version>1.0.16</version>
</dependency>    

When cloud run is starting, an exception occured :

WARN [io.agr.pool] (Agroal_18109070341) Datasource '<default>': Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

I have to say that cloud run is deployed by cloud build.

Could you help me to fix this issue... Thanks

2

2 Answers

1
votes

You need to use a unix_socket file looks like the following

postgres://user:password@/databasename?unix_sock=/cloudsql/projectshortcod:us-central1:pg-instance-name

you probably need to put this in the URL field of you library, but make sure that the library supports unix_socket databases.

0
votes

I got this working (for MySQL instead of Postgres; but should be similar) without using a unix_socket, but simply using a quarkus.datasource.jdbc.url=jdbc:mysql:///DB-NAME?ipTypes=PRIVATE&cloudSqlInstance=PROJECT-NAME:REGION:demo&socketFactory=com.google.cloud.sql.mysql.SocketFactory (and an quarkus.datasource.username= and quarkus.datasource.password=, as always).

The ?ipTypes=PRIVATE& is to connect to a Cloud SQL with only a private instead of a public IP from within GCP; omit that if you want to connect to a public IP, e.g. from your developer machine at home or work.

The trickiest part wasn't so much this but to set up all the required pre-requisites on GCP for this connection to work from a Quarkus service running on AppEngine Standard.

Quarkus Issue #9985 is for about documenting this.