PostgreSQL

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:

For instance edit the default.props file .

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

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

 Class.forName
val dm = DriverManager.getConnection

The full object:

object DBVendor extends ConnectionManager {
  def newConnection: Box[Connection] = {
    try {
      Class.forName
      val dm = DriverManager.getConnection
      Full
    } catch {
      case e : Exception => e.printStackTrace; Empty
    }
  }
  def releaseConnection {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.

Setup

Add to webapp/WEB-INF/web.xml

<resource-ref>
    <description>Database connection</description>
    <res-ref-name>lift</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Create new file webapp/WEB-INF/jetty-env.xml

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="lift" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>lift</Arg>
        <Arg>
            <New class="org.postgresql.ds.PGSimpleDataSource">
                <Set name="User">USER_NAME</Set>
                <Set name="Password">PASSWORD</Set>
                <Set name="DatabaseName">DATABASE_NAME</Set>
                <Set name="ServerName">SERVER_HOST</Set>
                <Set name="PortNumber">5432</Set>
            </New>
        </Arg>
    </New>
</Configure>

In build.sbt you have to add

env in Compile := Some(file.asFile)

and add following line to library dependencies

"org.eclipse.jetty" % "jetty-plus"        % "8.1.7.v20120910"  % "container,test"

Now, all you need is to add to Boot.scala

DefaultConnectionIdentifier.jndiName = Props.get openOr "lift"

Usage

Just use Mapper as you would otherwise.