Trying to connect postgresql heroku with play - postgresql

I'm trying to use heroku postgresql with play framework (version 2.5.9) but it doesn't work...
build.sbt
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
"org.webjars" % "bootstrap" % "2.3.1",
"org.hibernate" % "hibernate-entitymanager" % "5.2.3.Final",
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
conf/application.conf
ebean.default = ["models.*"]
db.default.driver=org.postgresql.Driver
db.default.url="postgres://ambpfzhcrpupmx:PeSnt07zQFVWKQrsqcRd1GzW6I#ec2-54-227-245-197.compute-1.amazonaws.com:5432/d4i5fck68498ce"
default.username = hereMyUserNameFromHeroku
default.password = "hereMyPassNameFromHeroku"
I obtain :
Caused by: org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "78.192.102.83", user "ambpfzhcrpupmx", database "d4i5fck68498ce", SSL off
I dont' understand why...
I'm trying to add "?ssl=true" at the end of my url but it doesn't work too... I obtain :
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Anybody can help me plz ? Thank :)

First, reset your Postgres username and password because you've posted them publicly and anyone can access your database now. Run this command:
$ heroku pg:credentials DATABASE --reset
Then change your conf/application.conf to use the JDBC_DATABASE_URL environment variable as per the Play Framework documentation.
db.default.url=${?JDBC_DATABASE_URL}
db.default.username=${?JDBC_DATABASE_USERNAME}
db.default.password=${?JDBC_DATABASE_PASSWORD}
The underlying problem is that your database URL does not enable SSL, and your Postgres JDBC dependency is too old for it to be turned on automatically. I recommend updating to this:
"org.postgresql" % "postgresql" % "9.4.1211"

Related

Slick can't connect to SQL Server Database

I'm running a Scala-Play back end and i'm trying to get slick 2.1.0 to connect to my microsoft sql server database. However i'm constantly getting "Can't Connect To Database" every-time i load up.
These are my settings i'm running slick 2.1.0
slick-extensions 2.1.0 gives me a "unknown artifact" in my IDE
libraryDependencies ++= Seq(
cache, ws, filters, jdbc, evolutions,
"com.typesafe.slick" %% "slick-extensions" % "2.1.0",
"com.typesafe.slick" %% "slick" % "2.1.0"
)
My Application.conf.
db.default {
driver = "com.typesafe.slick.driver.ms.SQLServerDriver"
url = """jdbc:sqlserver://SKYNET\DEV:40000"""
username = "XXX"
password = "XXXXXXXX"
}
Any help or push in the right direction would be greatly appreciated. The JDBC connection url is 100% not the problem because iv'e tested that JDBC string in my IDE to connect.
I think its because i can't get the driver from slick-extensions.
EDIT: Have the resolver in my built.sbt aswell.
EDIT UPDATE: I think after some Github slick reading iv'e progressed the new problem is "Cannot load Driver[Driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] cannot be instantiated.]"
Updated Code:
db.default {
slickdriver = "com.typesafe.slick.driver.ms.SQLServerDriver"
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url = "jdbc:sqlserver://SKYNET\\DEV:40000"
username = "XXXXX"
password = "XXXXXXXXXXXXXXXXXXXX"
}
"com.microsoft.sqlserver" % "mssql-jdbc" % "6.2.1.jre8",
Added into my libraryDependencies in SBT fixed it.

PlaySlick Database Connection to PostgreSQL

I just started working with Play Framework in Scala for a school project so I don't know much about the the Framework yet. I'm trying to connect my project to PostgreSql using slick but it is not working. I did exactly the same I have found in the Play website and in many tutorials but it doesn't work.
This is my DB config in the application.conf file
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://localhost:5432/IRProject"
slick.dbs.default.db.user="<my_username>"
slick.dbs.default.db.password="<my_password>"
1.sql file in conf/evolutions/default
# --- !Ups
CREATE TABLE Document(
id SERIAL PRIMARY KEY NOT NULL,
title VARCHAR(500),
date TIMESTAMP WITH TIME ZONE,
url VARCHAR(1000) NOT NULL ,
path VARCHAR(1000) NOT NULL ,
summary VARCHAR(5000) NOT NULL
);
# --- !Downs
DROP TABLE IF EXISTS Document;
My dependencies:
"org.webjars" % "jquery" % "2.1.3",
"com.typesafe.play" %% "play-slick" % "1.1.1",
"com.typesafe.play" %% "play-slick-evolutions" % "1.1.1",
"postgresql" % "postgresql" % "9.1-901.jdbc4"
When I run using activator, I don't see any logs about DB connection or any error at all. The application just starts up but without any tables in the database.
Does any one know a reason for this?
Should anyone come across this problem, these are the solutions I found.
I placed the slick db config in the db{} block inside the config file but that is wrong, it should be outside directly inside the config file.
I needed to add a test query that can be used to test the connection e.g
slick.dbs.default.db.connectionTestQuery="SELECT TRUE"
After this it worked.

Play Framework trying to access DB with windows username

I have a db.conf which looks like this in the conf folder:
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://62.210.145.112/babybets"
db.default.username=postgres
db.default.password="my_password"
It is included in the application.conf:
include "db.conf"
Infos are definitely correct, as I use this very same jdbc connection string, user/pass to connect the Intellij DB view to the database.
When I try to access any page, I get an error:
play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94) ~[play_2.10-2.3.8.jar:2.3.8]
at play.api.Configuration.reportError(Configuration.scala:743) ~[play_2.10-2.3.8.jar:2.3.8]
at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:247) ~[play-jdbc_2.10-2.3.2.jar:2.3.2]
at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:238) ~[play-jdbc_2.10-2.3.2.jar:2.3.2]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library-2.10.5.jar:na]
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "John"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:293) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) ~[postgresql-9.1-901-1.jdbc4.jar:na]
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) ~[postgresql-9.1-901-1.jdbc4.jar:na]
In the above you can see it tries to connect to the database using my windows login "John".
I went through the project with agent Ransack and tried the following (case insensitive) regex:
[^\\]john[^\\]
It only matches with the above error log.
Question: Why is play using my windows login to Connect DB? How to prevent that ?
Just for info my build.sbt looks like this:
name := "babyBets2"
version := "1.0"
lazy val `babybets2` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.10.5"
libraryDependencies ++= Seq( cache , ws )
libraryDependencies ++= Seq("postgresql" % "postgresql" % "9.1-901-1.jdbc4",
"com.typesafe.play" %% "play-slick" % "0.8.0",
"ws.securesocial" %% "securesocial" % "2.1.4" )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
Ok this is crazy, there is a difference between Play versions:
https://www.playframework.com/documentation/2.4.x/ScalaDatabase
https://www.playframework.com/documentation/2.3.x/ScalaDatabase
db.default.username=postgres
vs
db.default.user=postgres
So apparently, if no user is provided, play displays no useful error messages and tries to connect you with windows login!

How to configure HikariCP for Slick 3.0.0 RC1 on Typesafe conf

I have a Play Application based on the play-scala Typesafe template (Play Scala Seed), and tried to add Slick 3.0.0 to the project and connect to a PostgreSQL database.
First I added the dependencies to build.sbt:
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.0-RC1",
"postgresql" % "postgresql" % "9.1-901.jdbc4"
)
Then added the database configuration on application.conf:
brDb = {
dataSourceClass = org.postgresql.ds.PGSimpleDataSource
url = "jdbc:postgresql://localhost:5432/test"
user = "postgres"
password = "postgres"
numThreads = 10
}
Note that I haven't disabled explicitly the pooling, so it is enabled by default, and will try to use HikariCP, because as of Slick 3.0.0 RC1, HikariCP support exists and pooling using it is enabled by default.
And in my DAO object, tried to get the database connection like this:
Database.forConfig("brDb")
When I run the app using activator run, I get this error:
RuntimeException: java.lang.NoClassDefFoundError:
com/zaxxer/hikari/HikariConfig
Then I tried adding HikariCP as a dependency in build.sbt:
libraryDependencies ++= Seq(
// ...
"com.zaxxer" % "HikariCP" % "2.3.3",
// ...
)
Cleaned and recompiled the app using activator clean compile, and runned it again, but I get another error:
RuntimeException: java.lang.UnsupportedClassVersionError: com/zaxxer/hikari/HikariConfig
I think I am missing some configuration, but I am not sure and have not found more info about it. How should I set up the configuration to get the connection pool working?
That error means that the HikariCP package is compiled for a JRE newer than the one you#re running on. And in fact, if you look at the homepage, you'll see that the version you are using is this:
Java 8 maven artifact:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.3.5</version>
<scope>compile</scope>
</dependency>
I suppose you're running on Java 7. For that, add the following to your build.sbt:
libraryDependencies ++= Seq(
"com.zaxxer" % "HikariCP-java6" % "2.3.3",
)
Btw, I have found out that the configuration structure that you specified above does not actually work as of Slick 3.0.0. What worked for me was to specify the database configuration as described in the documentation http://slick.typesafe.com/doc/3.0.0/database.html#using-typesafe-config, otherwise Slick will assume some default values for the configuration. I'm mainly talking about the "properties" field.
So something like this should work:
mydb = {
dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
properties = {
serverName = "postgresdb"
portNumber = "5432"
databaseName = "mydb"
user = "myuser"
password = "secret"
}
numThreads = 10
}

Postgres + Play 2.2.1 Java - Driver not found : [ org.postgresql.Driver ]

I'm trying to connect the sample 'computer-database-jpa' to postgres , but I cant.
https://github.com/playframework/playframework/tree/master/samples/java/computer-database-jpa
Could you help me?
My steps:
add jar (build path> configure build path > add external jars ... )
add to build.sbt: "postgresql" % "postgresql" % "9.3-1100.jdbc4"
add to application.conf:
db.default.driver=org.postgresql.Driver
db.default.url=" jdbc:postgresql://localhost:5432/teste "
db.default.user="postgres"
db.default.password="postgre"
Result:
Browser message: Driver not found : [ org.postgresql.Driver ]
Where is the problem ?
PostgreSQL artifacts have moved to a new group ID org.postgresql.
"org.postgresql" % "postgresql" % "9.3-1100-jdbc4"