Ozgur Dogus
Ozgur Dogus

Reputation: 921

How to set the default PostgreSQL schema in Play?

We are using Play Framework 2.1 in our web application. We want to explicitly set the database schema (not public schema) in our PostgreSQL database that's the application's database. How can I set it ?

Upvotes: 2

Views: 1854

Answers (3)

ForeverLearner
ForeverLearner

Reputation: 2103

Playframework 2.8.x using Scala example:

We can add the below entry in application.conf:

db {
  # You can declare as many datasources as you want.
  # By convention, the default datasource is named `default`

  default.driver = org.postgresql.Driver
  default.url = "jdbc:postgresql://localhost/postgres?currentSchema=backoffice"
  default.username = "user"
  default.password = "password"
}

Play framework will create a default connection pool with these parameters. Postgres driver basically gives the ability to define the default schema in conneciton url using ?currentSchema=backoffice from version 9.4 onwards.

A Dao object can use this database as below:

import com.google.inject.Inject
import play.api.db.{DBApi, Database, DefaultDBApi}

class PostgresDao @Inject()(backofficeDB : Database) {
  val backofficeDb = backofficeDB
  //some more methods

}

Upvotes: 0

user330315
user330315

Reputation:

If your tables are all located outside of the public schema, the best thing to do, is to change the search_path for your application user:

alter user your_appuser set search_path = 'schema1';

If you have multiple schemas, you can add all of them:

alter user your_appuser set search_path = 'schema1,schema2,public';

Don't forget to commit this statement. The change will only have affect after the user logs in the next time. Existing connections will not be affected.

Upvotes: 2

Wayan Wiprayoga
Wayan Wiprayoga

Reputation: 4562

As far as I know, from what I have tried before. You should define your schema name for each Model you want to. It should be like this:

import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(schema = "schema2")
public class TableOnSchema2 extends Model {
  ...
}

Maybe this solution would make an additional effort to define each Model with schema name. Because, I don't know whether there is configuration value can be set for specifying default database scheme for the application. But it works for me!

Hope this would help you.. :)

Upvotes: 3

Related Questions