PostgreSQL CREATE SCHEMA

Learn via video courses
Topics Covered

Overview

In PostgreSQL, a schema is a logical container that holds a collection of database objects, such as tables, views, indexes, and functions. Schemas provide a way to organize and manage database objects, making it easier to manage large and complex databases. The CREATE SCHEMA statement is used to create a new schema within a PostgreSQL database. This article dives into the syntax, parameters, usage, examples, and frequently asked questions related to the CREATE SCHEMA statement in PostgreSQL.

CREATE SCHEMA Statement in PostgreSQL

Syntax

The syntax for creating a schema in PostgreSQL using the CREATE SCHEMA statement is as follows:

Parameters

  • schema_name:

    This is a mandatory parameter that specifies the name of the schema you want to create.

  • AUTHORIZATION Owner:

    Optional parameter that designates the owner of the schema. The owner is usually a PostgreSQL role that will have all privileges on the objects within the schema.

  • schema_element:

    This is an optional parameter that allows you to include specific database objects within the schema during its creation. You can include multiple schema elements separated by commas.

How to Use CREATE SCHEMA in PostgreSQL

Using the CREATE SCHEMA statement in PostgreSQL is straightforward. To create a new schema, you simply need to provide the name of the schema you want to create. Here's an example:

In this example, a schema named "sales" is created. By default, the user executing the statement becomes the owner of the schema. You can also explicitly specify an owner using the AUTHORIZATION clause:

In this case, the "reports" schema will be owned by the PostgreSQL role "analyst".

To include specific objects within the schema during its creation, you can use the schema_element parameter. For instance, to create a schema named "hr" with a table named "employees", you would execute:

Output: output

Examples

Example 1: Creating a Schema

Let's create a schema named "inventory" without specifying an owner:

Output:

Example 2: Creating a Schema with an Owner

In this example, we'll create a schema named "logs" owned by the "logger" role:

Output:

Example 3: Creating a Schema with Objects

Suppose we want to create a schema named "university" containing a table named "students":

Output:

FAQs

Q. Can I change the owner of an existing schema?

A. Yes, you can use the ALTER SCHEMA statement to change the owner of a schema.

Q. Are schema names case-sensitive?

A. No, schema names in PostgreSQL are not case-sensitive. However, it's recommended to use lowercase letters for consistency.

Q. Can I create nested schemas within other schemas?

A. Yes, you can create nested schemas within other schemas. For example, you can create a schema like "parent.child" to indicate nesting.

Conclusion

  • The CREATE SCHEMA statement in PostgreSQL is a powerful tool for organizing and managing database objects within logical containers.
  • It allows you to create schemas with specific owners and include various database objects within them.
  • By using schemas, you can better structure your database, manage access privileges, and avoid naming conflicts.
  • Understanding how to use the CREATE SCHEMA statement empowers you to design more efficient and organized database systems.