Cloud SQL Postgres找不到适用于JDBC的驱动程序:postgres://google/ [英] Cloud SQL Postgres No suitable driver found for jdbc:postgres://google/

查看:40
本文介绍了Cloud SQL Postgres找不到适用于JDBC的驱动程序:postgres://google/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前谢谢!

我是GCP新手,正在尝试使用Cloud SQL Postgres。 我有一个已创建的数据库,并且希望使用Java或Scala在其中创建/更新/删除表。

我正在尝试使用‘com.google.cloud.sql’包中的postgres-Socket-Factory。 但在创建连接时,我收到一个错误,

"java.sql.SQLException: No suitable driver found for jdbc:postgres://google/"

以下是我的代码

def getConnection(url:String) : Connection = {
  Class.forName("org.postgresql.Driver")
  import java.sql.DriverManager
  DriverManager.getConnection(url)
}

Java/Scala客户端通过什么方式从Cloud SQL访问数据库?

按照备注中的说明进行操作并更改URL后是的,该操作有效但失败

    Caused by: java.lang.RuntimeException: Unable to obtain credentials to communicate with the Cloud SQL API
    at com.google.cloud.sql.core.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:600)
    at com.google.cloud.sql.core.SslSocketFactory.getInstance(SslSocketFactory.java:147)
    at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:91)
    at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:454)
    at org.postgresql.Driver.connect(Driver.java:256)
    ... 6 more
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
    at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredential(DefaultCredentialProvider.java:98)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191)
    at com.google.cloud.sql.core.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:598)
    ... 15 more

如何解决此故障?

问候,

推荐答案

gcp上的大多数库使用Application Default Credentials(Adc)策略来处理凭据。这个项目的README提到了这个库。Cloud SQL JDBC套接字工厂使用这些凭据来根据数据库对连接进行身份验证。

最快捷的方法是使用gcloud auth application-default login,它会将您的个人凭据设置为默认凭据。但是,最安全、最安全的方法是为应用程序创建Service Account,授予它"Cloud SQL Client"IAM角色,并使用GOOGLE_APPLICATION_CREDENTIALS环境变量将服务帐户密钥的位置传递给库。

这篇关于Cloud SQL Postgres找不到适用于JDBC的驱动程序:postgres://google/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆