Search code examples
databasescalaplayframeworkdriveramazon-redshift

Cannot connect to Redshift database with a driver even though play.ap.db.DB can do this for the same driver


I am trying to connect to a redshift server and run some sql commands. Here is the code that I have written:

Class.forName("org.postgresql.Driver")
val url: String = s"jdbc:postgres://${user}:${password}@${host}:${port}/${database}"
val connection: Connection = DriverManager.getConnection(url, user, password)
val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val setSearchPathQuery: String = s"set search_path to '${schema}';"
statement.execute(setSearchPathQuery)

But I am getting the following error:

java.sql.SQLException: No suitable driver found for jdbc:postgres://user:password@host:port/database

But when I am using play framework's default database library with the same configuration, then I am able to connect to database successfully. Below is the configuration for the default database:

db.default.driver=org.postgresql.Driver
db.default.url="postgres://username:password@hostname:port/database"
db.default.host="hostname"
db.default.port="port"
db.default.dbname = "database"
db.default.user = "username"
db.default.password = "password"

Solution

  • The problem was with the url. The correct format for the url is:

    jdbc:postgresql://hostname:port/database