目次

Version 6, last updated by Chaba at Nov 02 04:28 UTC

The default archetype uses Apache Derby DB. Below is described how to use PostgreSQL instead.

Dependency

SBT users

Add dependency to your project description:

val lift_postgresql = "postgresql" % "postgresql" % "8.4-701.jdbc4"

Maven users

Add dependency to your pom.xml:

<dependency>
 <groupId>postgresql</groupId>
 <artifactId>postgresql</artifactId>
 <version>8.4-701.jdbc4</version>
</dependency>

PostgreSQL driver repository

See the full driver list here: http://repo1.maven.org/maven2/postgresql/postgresql/

Setup with Lift properties – recommended

Take advantage of the Lift configuration management; set up the database properties in the according property file:

  • src\main\resources\props\default.props
  • src\main\resources\props\production.default.props
  • src\main\resources\props\test.default.props

For instance edit the default.props file (for other configuration files refer to net.liftweb.util.Props).

db.driver=org.postgresql.Driver
db.url=jdbc:postgresql:DATABASENAME
db.user=USERNAME
db.password=PASSWORD

or

db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://MYHOST:MYPORT/MYDATABSE?user=MYUSER&password=MYPASSWORD

List of the PostgreSQL connection parameters: http://jdbc.postgresql.org/documentation/84/connect.html

Setup (hard coded)

Change the 2 Derby lines in src/main/scala/bootstrap/liftweb/Boot.scala

 Class.forName(classOf[org.postgresql.Driver].getName)
val dm = DriverManager.getConnection("jdbc:postgresql://HOST:PORT/DATABSE?user=USER&password=PASSWORD")

The full object:

object DBVendor extends ConnectionManager {
  def newConnection(name: ConnectionIdentifier): Box[Connection] = {
    try {
      Class.forName(classOf[org.postgresql.Driver].getName)
      val dm = DriverManager.getConnection("jdbc:postgresql://HOST:PORT/DATABSE?user=USER&password=PASSWORD")
      Full(dm)
    } catch {
      case e : Exception => e.printStackTrace; Empty
    }
  }
  def releaseConnection(conn: Connection) {conn.close}
}

List of the PostgreSQL connection parameters: http://jdbc.postgresql.org/documentation/84/connect.html

Note: The classOf construct indicates the driver class as a Scala name, not as a string literal. This displays an error message during compilation if the dependency is not correctly set.

Usage

Just use Mapper as you would otherwise.