Search code examples
postgresqlgoogle-cloud-platformgoogle-cloud-sqlquarkuscloud-sql-proxy

Quarkus Datasource using Unix Socket is ignored


I am trying to connect quarkus reactive datasource using (Google Cloud SQLproxy). You are able to run the cloud_sql_proxy as unix socket available connection.In this way:

./cloud_sql_proxy -dir=/tmp/cloudsql -instances=proyectA:europe-west1:cloudsql-sandbox-ephemeral -credential_file=../security/proyectA-credentials.json &
2020/06/30 12:39:27 Listening on /tmp/cloudsql/proyectA:europe-west1:cloudsql-sandbox-ephemeral/.s.PGSQL.5432 for proyectA:europe-west1:cloudsql-sandbox-ephemeral
2020/06/30 12:39:27 Ready for new connections

Then try to connect with this application.properties:

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=123456
quarkus.datasource.reactive.url=postgresql:///postgres?cloudSqlInstance=/tmp/cloudsql/proyectA:europe-west1:cloudsql-sandbox-ephemeral/.s.PGSQL.5432
quarkus.datasource.jdbc=false
quarkus.datasource.reactive=true

running the Quarkus APP, get the error: ion: Conexión refused: localhost/127.0.0.1:5432 Caused by: java.net.ConnectException: Conection refused

But, the message appears to try connect using TCP instead Unix socket. It is posible connect quarkus datasource via unix socket?

Java code is so simple

@Inject
private io.vertx.mutiny.pgclient.PgPool client;

(I can connect to cloud_sql_proxy using TCP without problems, but I need to configure unix socket to be able to deploy my quarkus app to cloud run with the cloud_sql_proxy)


Solution

  • To connect using a UNIX domain socket, the Reactive Pg client needs Netty native support.

    First add the native transport jars to your project:

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-transport-native-epoll</artifactId>
        <classifier>linux-x86_64</classifier>
    </dependency>
    
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-transport-native-kqueue</artifactId>
        <classifier>osx-x86_64</classifier>
    </dependency>
    

    Then enable native transport in the Quarkus config file:

    quarkus.vertx.prefer-native-transport=true
    

    Finally, fix the reactive db url:

    quarkus.datasource.reactive.url=postgresql://:5432/postgres?host=/tmp/cloudsql/proyectA:europe-west1:cloudsql-sandbox-ephemeral