玩 Java - 无法连接到数据库 postgreSQL

Posted

技术标签:

【中文标题】玩 Java - 无法连接到数据库 postgreSQL【英文标题】:Play Java - cannot connect do database postgreSQL 【发布时间】:2016-01-31 12:22:04 【问题描述】:

我使用激活器创建了新的 play-java 应用程序。现在我需要将它连接到一个 postgreSQL 数据库。无论我尝试了什么,我总是得到这个错误:

Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]

这是我的 build.sbt 以及最新的依赖项:

libraryDependencies += "postgresql" % "postgresql" % "9.1-901-1.jdbc4"
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.38"

这里是 application.conf:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/table4you"
db.default.user="postgres"
db.default.password="test"

我什至试过这个:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://postgres:test@localhost:6789/table4you"

这也不起作用。 之后我尝试检查 mySQL 是否可以工作,并在 build.sbt 中添加了 mysql 依赖项并将驱动程序和 URL 更改为:

#db.default.driver=com.mysql.jdbc.Driver
#db.default.url="jdbc:mysql://localhost/table4you"
#db.default.user="root"
#db.default.password=""

它成功了! 但我真的必须让它与 postgres 一起工作。现在我假设问题出在数据库本身,而不是 application.conf 文件。我启动数据库的方式是,我打开 pgAdmin III,并在其中连接到我的服务器和 table4you 数据库(通过双击它们)。 我应该使用其他方式来启动 postgresql 数据库吗?我该怎么做才能使它与 postgresql 一起使用?

编辑:我试过这个,但我又得到了同样的错误: 我在 heroku 上创建了一个数据库,并使用了我在那里的连接设置(主机名、用户名、密码和数据库名称)来使用在线数据库而不是 localhost 数据库。可惜输出和以前一样:(。

这是错误的堆栈跟踪:

Unexpected exception
CreationException: Unable to create injector, see the following errors:

1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
  while locating play.api.db.DBApiProvider
  while locating play.api.db.DBApi
    for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28)
  at play.db.DefaultDBApi.class(DefaultDBApi.java:28)
  while locating play.db.DefaultDBApi
  while locating play.db.DBApi
    for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
  while locating play.db.DBModule$NamedDatabaseProvider
  at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
  at play.db.DBModule.bindings(DBModule.java:40):
Binding(interface play.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.db.DBModule$NamedDatabaseProvider@70606a)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
Caused by: Configuration error: Configuration error[Cannot connect to database [default]]
    at play.api.Configuration$.configError(Configuration.scala:178)
    at play.api.Configuration.reportError(Configuration.scala:829)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:48)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
    at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
    at play.api.db.DBApiProvider.get(DBModule.scala:62)
    at play.api.db.DBApiProvider.get(DBModule.scala:58)
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
    at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
    at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
    at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
    at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
    at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:104)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:267)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:56)
    at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:54)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:93)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:80)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
    at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:62)
    at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:984)
    at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
    at com.google.inject.util.Providers$GuicifiedProviderWithDependencies$$FastClassByGuice$$2a7177aa.invoke(<generated>)
    at com.google.inject.internal.cglib.reflect.$FastMethod.invoke(FastMethod.java:53)
    at com.google.inject.internal.SingleMethodInjector$1.invoke(SingleMethodInjector.java:57)
    at com.google.inject.internal.SingleMethodInjector.inject(SingleMethodInjector.java:91)
    at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:93)
    at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:80)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
    at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
    at com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:174)
    at com.google.inject.internal.Initializer.injectAll(Initializer.java:108)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:174)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
    at com.google.inject.Guice.createInjector(Guice.java:96)
    at com.google.inject.Guice.createInjector(Guice.java:73)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:153)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:150)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:150)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:121)
    at scala.Option.map(Option.scala:146)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:121)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:119)
    at scala.util.Success.flatMap(Try.scala:230)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:119)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:111)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: Configuration error: Configuration error[Exception during pool initialization]
    at play.api.Configuration$.configError(Configuration.scala:178)
    at play.api.PlayConfig.reportError(Configuration.scala:1048)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:69)
    at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
    at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:124)
    at play.api.db.DefaultDatabase.dataSource(Databases.scala:122)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:143)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:139)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:44)
    ... 67 more

【问题讨论】:

“不起作用”是什么意思?任何错误消息或堆栈跟踪? 我在运行应用程序时收到“无法连接到数据库 [默认]”消息。我在问题的底部添加了堆栈跟踪。 您可以尝试使用命令行工具连接到 postgres:psql -h localhost table4you postgres 另外,您可以尝试为主机名指定127.0.0.1 而不是localhost psql -h localhost table4you postgres 产生此输出:psql: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 我现在很困惑当我安装 postgresql 时,我将 6789 设置为默认端口。现在我将重新安装 postgres 并使用 5432 作为默认端口。我会保持联系 【参考方案1】:

它现在已修复,并且可以正常工作。问题是我使用了旧的 postgresql 依赖项(2011 年制造),一旦我用新的依赖项(2015 年 12 月)替换它,错误就消失了。 感谢@robinj 的帮助..

【讨论】:

以上是关于玩 Java - 无法连接到数据库 postgreSQL的主要内容,如果未能解决你的问题,请参考以下文章

设置 PostgreSQL,无法连接到数据库 postgres

Createuser:无法连接到数据库 postgres:致命:角色“tom”不存在

Heroku Postgres 升级后 Flyway 无法连接到数据库

Docker 中的 Postgres 服务器无法连接到 Spring Boot 应用程序

节点服务器无法通过 Docker 连接到 Postgres,使用 TypeORM

无法从 .NET 应用程序 (Npgsql) 连接到 Heroku Postgres