Using multiple schemas
We can separate groups of tables into namespaces, referred to as schemas by PostgreSQL. In many ways, they can be thought of as being similar to directories, although that is not a precise description, and schemas are not arranged in a hierarchy.
Getting ready
Make sure you’ve read the Deciding on a design for multitenancy recipe so that you’re certain that this is the route you wish to take. Other options exist, and they may be preferable in some cases.
How to do it…
Follow these steps:
- Schemas can easily be created using the following commands:
CREATE SCHEMA finance; CREATE SCHEMA sales;
- Then, we can create objects directly within those schemas using fully qualified names, like this:
CREATE TABLE finance.month_end_snapshot (.....)
The default schema where an object is created is known as
current_schema
. We can find out what our current schema is by using the following query...