播放无法连接到(PostgreSQL)数据库[​​默认] [英] Play Cannot connect to (PostgreSQL) database [default]

查看:259
本文介绍了播放无法连接到(PostgreSQL)数据库[​​默认]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要

CreationException: Unable to create injector, see the following errors: 
1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
[...]
2) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]

最初,我将MySQL数据库与Play Framework应用程序配合使用,但我想将其更改为PostgreSQL,这就是问题开始出现的时候.我已经在Ubuntu计算机上安装了它们,并更改了播放配置以使用Postgres(将"postgresql" % "postgresql" % "9.1-901-1.jdbc4",添加到build.sbt,并将db.default属性更改为引用Postgres).确切的application.conf是:

Originally I had MySQL database used with Play Framework app which worked, but I wanted to change it to PostgreSQL and that's when problems started appearing. I've installed them both on my Ubuntu computer and changed play config to use Postgres (added "postgresql" % "postgresql" % "9.1-901-1.jdbc4", to build.sbt and changed db.default properties to reference Postgres). Exact application.conf is:

db.default.driver="org.postgresql.Driver"
db.default.url="jdbc:postgres://localhost:5432/playdb"
db.default.username="luka"
db.default.password="test"

我已经用密码测试和数据库playdb手动创建了luka用户.我也尝试过postgres用户,但无济于事.

I have manually created user luka with password test and database playdb. I have tried with postgres user as well with no avail.

还有什么让我感到烦恼的是,MySQL现在也不会因为相同的错误而工作.我创建了一个新项目,只修改了conf中的db.default参数,它以相同的方式失败.注释掉application.conf会使它消失,所以这绝对是问题所在.我已经检查了PostgreSQL日志(/var/log/postgresql/postgresql-9.4-main.log),只有看起来不正确的行是[unknown]@[unknown] LOG: invalid length of startup packet.它会出现多次,但并非每次刷新项目时都会出现(我什至不确定它是否与之相关).我已经从PC上删除了mysql-server,希望一切都能神奇地修复自己.没有.

What bugs me more, MySQL won't work either now with the same error. I have created new project with only modifying db.default params in conf and it fails in the same manner. Commenting out application.conf makes it go away, so that's definitely the problem. I have checked PostgreSQL logs (/var/log/postgresql/postgresql-9.4-main.log) and only line that doesn't seem right is [unknown]@[unknown] LOG: invalid length of startup packet. It appears multiple times, but not every time I refresh project (I'm not even sure it's related). I have removed mysql-server from my PC hoping everything will magically fix itself. It didn't.

想法?

我正在使用Play 2. 4. 6和IntelliJ IDEA15.项目是使用Activator创建的,并将源导入IDEA(使用SBT模型).

I'm using Play 2. 4. 6 and IntelliJ IDEA 15. Project is created using Activator and importing sources to IDEA (using SBT model).

编辑,当我在application.conf中添加db.default.hikaricp.connectionTestQuery = "SELECT 1"时,也会出现错误.

EDIT I'm also getting errors when I add db.default.hikaricp.connectionTestQuery = "SELECT 1" to my application.conf.

推荐答案

因此,明确的答案是:

首先,数据库网址有误,应为db.default.url="jdbc:postgresql://localhost:5432/playdb",如 chabeee指出的.这是db.default.url的唯一正确格式(所以没有jdbc:postgresql://username:pasword:localhost/dbname或类似格式,正如我在其他地方看到的那样).

First, there's a mistake in the database url, it should be db.default.url="jdbc:postgresql://localhost:5432/playdb" as chabeee pointed out. It's the only correct format for db.default.url (so no jdbc:postgresql://username:pasword:localhost/dbname or similar, as I've seen suggesting on other places).

第二个,更棘手的是,驱动程序中有一个错误,因为 Salem指出了,并且解决方法正在添加application.conf.
但是,该错误已在9.1-903以后的版本中修复(很好,已实现该解决方法).问题是,在版本9.1-901中,postgresql在存储库中更改了它的groupID,现在,它被org.postgresql引用.比变通办法更好的解决方案是将依赖关系更新为"org.postgresql" % "postgresql" % "9.4-1206-jdbc4"(当前版本

Second, more tricky, is that there's a bug in the driver as Salem pointed out and workaround is adding db.default.hikaricp.connectionTestQuery = "SELECT 1" to application.conf.
However, that bug is fixed (well, that workaround is implemented) in versions more recent than 9.1-903. The catch is, after version 9.1-901 postgresql changed its groupID in the repos and now it's referenced by org.postgresql. A better solution than the workaround would be updating dependencies to "org.postgresql" % "postgresql" % "9.4-1206-jdbc4" (current version, MVNrepository). Append the appropriate jdbc version to the most recent PostgreSQL driver (4 for Java 6, 41 for Java 7, 42 for Java 8).

我的最后一个application.conf:

db.default.driver="org.postgresql.Driver"
db.default.url="jdbc:postgresql://localhost/playdb" #the port is optional
db.default.username="luka"
db.default.password="test"

build.sbt中的libraryDependencies:

libraryDependencies ++= Seq(
  jdbc,
  "org.postgresql" % "postgresql" % "9.4-1206-jdbc42",
  cache,
  javaWs
)

UPDATE 2017: 我现在才注意到,在写完此答案后不久,他们更改了版本控制方案并删除了-jdbc [code]片段,将其替换为.jre6,.jre7或什么都没有,显然这意味着它适用于最新的Java版本(我没有找到支持该声明的任何内容,但确实可行).在2017年2月,他们又再次更改了版本方案,并从主要版本跳了下来. 9至42,以"org.postgresql" % "postgresql" % "42.1.3"(或相应的"org.postgresql" % "postgresql" % "42.1.3.jre7"/"org.postgresql" % "postgresql" % "42.1.3.jre6")表示的当前版本(截至2017年7月17日)

UPDATE 2017: I have only now noticed that not long after writing this answer they changed versioning scheme and removed -jdbc[code] fragment, replacing it with .jre6, .jre7 or nothing, apparently meaning it's meant for the latest Java version (I haven't found anything supporting this claim, but it works). Yet again in February 2017 they changed the version scheme again and jumped from major version 9 to 42, making the current version (as of 17 July 2017) denoted by "org.postgresql" % "postgresql" % "42.1.3" (or, accordingly, "org.postgresql" % "postgresql" % "42.1.3.jre7" / "org.postgresql" % "postgresql" % "42.1.3.jre6")

这篇关于播放无法连接到(PostgreSQL)数据库[​​默认]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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