Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default schema for Vertica Database

I am building a web application using Play! with Vertica database as back-end. The JDBC connection string for Vertica contains the server and database name, but my tables are under a specific schema (say "dev_myschema"). Thus, I should refer to my table as "dev_myschema.mytable". There is an exact copy of all these tables in a production schema as well (say "prod_myschema") with real data.

I would like to set this schema name in the configuration file so that it is easy to switch between these two schema. For now, I have a getConnection method in a helper class, that does DB.getConnection() and sets the configured schema as the default schema for that connection object. However, the same does not help in other model classes where it is mentioned along with its Entity annotation (@Entity @Table(name=dev_myschema.mytable))

Is there a way by which I can specify the schema name in the configuration file and have it read by the connection method as well as the model annotations?

Thanks.

like image 941
Ananth Avatar asked May 27 '11 06:05

Ananth


People also ask

How do you use schema in vertica?

Each user session has a search path of schemas. Vertica uses this search path to find tables and user-defined functions (UDFs) that are unqualified by their schema name. You can use the CURRENT_SCHEMA function to display the name of the current schema (i.e., the first “valid” schema in the user's search path).

What is database default schema?

The dbo schema is the default schema of every database. By default, users created with the CREATE USER Transact-SQL command have dbo as their default schema. The dbo schema is owned by the dbo user account. Users who are assigned the dbo as default schema don't inherit the permissions of the dbo user account.

How do I set the default schema in postgresql?

After creating a new schema, you can make it a default schema instead of a public schema. To make a schema default, the search_path variable is used. Search_path variable defines the schema order in which they are searched when an object is referenced without any schema name.


1 Answers

Eugene got it almost correct, but was missing an underscore. The correct Vertica SQL syntax to set the default schema is:

set search_path to dev_myschema

As Eugene suggested, if you are using low-level JDBC, as soon as you create your Connection object you can do:

conn.createStatement().executeUpdate("set search_path to " + schemaName);
like image 176
Eamonn O'Brien-Strain Avatar answered Sep 22 '22 22:09

Eamonn O'Brien-Strain