Create Schema - (PostgreSQL Schema)
A schema is a named collection of database objects, including tables, views, indexes, data types, functions, and operators. In PostgreSQL, a schema is a namespace that contains named objects. In this tutorial, we will discuss how to create a schema in PostgreSQL.
Syntax
CREATE SCHEMA schema_name AUTHORIZATION user_name;
schema_name
: The name of the schema to create.user_name
: The name of the user who will own the schema.
Example
Let's create a new schema named "sales" in PostgreSQL.
CREATE SCHEMA sales AUTHORIZATION postgres;
Output
CREATE SCHEMA
Explanation
In the above example, we used the CREATE SCHEMA
statement to create a schema named "sales". We also specified "postgres" as the user who will own the schema.
Use
Schemas are useful in PostgreSQL for organizing database objects and providing access control. By using schemas, different users or applications can have their own namespace and access only the objects in that namespace.
Important Points
Here are a few important points to keep in mind when creating schemas in PostgreSQL:
- Each schema can have its own set of permissions and access control rules, allowing granular control over who can access each schema.
- Objects within a schema can be accessed using the schema name as a prefix. For example,
sales.orders
would refer to the "orders" table in the "sales" schema. - The default schema for a user is typically their own name. However, this can be changed by setting the
search_path
variable. - Schemas can also be used to organize data by application or department, making it easier to manage large databases with many objects.
Summary
In this tutorial, we covered how to create a schema in PostgreSQL using the CREATE SCHEMA
statement. We discussed the syntax, example, output, explanation, use, and important points of creating a schema in PostgreSQL. With this knowledge, you can now create schemas in your PostgreSQL databases to organize objects and provide access control.