Play Cannot connect to (PostgreSQL) database [default]

六眼飞鱼酱① 提交于 2020-01-03 09:20:10

问题


I'm getting

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]]

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"

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

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.

Ideas?

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

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


回答1:


So, the definite answer is:

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).

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).

My final 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"

And libraryDependencies in build.sbt:

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

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")




回答2:


From your stacktrace

JDBC4 Connection.isValid() method not supported, connection test query must be configured

Basically this is a problem with your driver (it does not support that method), but you should be able to work around it specifying a test query in your application.conf:

play.db.default.hikaricp.connectionTestQuery = "SELECT 1"



回答3:


Try to replace this line:

db.default.url="jdbc:postgres://localhost:5432/playdb"

to this:

db.default.url="jdbc:postgresql://localhost:5432/playdb"

Hope, it helps.




回答4:


Are you by any chance on a Mac, and do you have a postgresql .jar file in your /Library/Java/Extensions directory?

Edit: Sorry, I'm new. I encountered the "JDBC4 Connection.isValid() method not supported" message while using Hikari on my Mac, and I solved it by locating and removing a JDBC3 .jar file in that extensions directory. I don't know how it got there (probably when I installed PostgreSQL or some related tool) but I found that it would get loaded instead of any JDBC4 .jar file on my classpath. Maybe something similar happens in Ubuntu. Probably worth checking, as other JDBC4-related problems could arise besides "isValid()".



来源:https://stackoverflow.com/questions/34323835/play-cannot-connect-to-postgresql-database-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!