PostgreSQL ALTER Schema

In this section, we are going to learn PostgreSQL ALTER SCHEMA command, which is used to change the description of a schema.

ALTER SCHEMA command

The alter schema command will allow us to modify the schema's definition. For example, we can also rename a schema with the help of the below alter schema command.

The syntax of altering schema command is as follows:

We have the following parameters which are used in the above syntax:

ParametersDescription
Alter SchemaIt is a keyword which is used to change the description of a schema
Schema_NameIt is used to describe the name of the schema, which we need to rename it.
New_NameThis parameter is used to define the new name of the schema, and we cannot start the new name with pg_, because in PostgreSQL, such names are kept for system schemas.

Note: For executing the above command, we must be the owner of the schema, and we also have access to CREATE privilege for the database.

Apart from renaming a schema, in the ALTER SCHEMA, we can also modify the owner of a schema for the new one with the below command's help:

We have the following parameters which are used in the above syntax:

ParametersDescription
Schema_NameFor modifying the owner in the ALTER SCHEMA condition, we will describe the name of the schema.
New_ownerIt is used to define the new owner in the OWNER TO condition.

Examples of PostgreSQL ALTER SCHEMA command

Let us see few examples where we can understand how to use the ALTER SCHEMA command in PostgreSQL. These below examples are based on the schema which we created in the PostgreSQL CREATE SCHEMA section.

To rename a schema using ALTER SCHEMA command

This below example is explained to rename a schema with the help alter schema command.

Here, we are going to rename the myschema schema to Schema1 schema:

Output

Once we execute the above command, we will get the below Output window:

PostgreSQL ALTER Schema

In the same way, the following example is used to describe the renames for the Company schema to the department:

Output

After executing the above command, we will get the below output:

PostgreSQL ALTER Schema

To modify the owner of a schema using alter schema command

In this particular example, we will modify the owner of the Schema1 from myschema to Postgres with the help of Alter schema:

PostgreSQL ALTER Schema

And then, we will execute the user-created schema command:

Note: In PostgreSQL, the pg_catalog is the typical metadata and core schema used by Postgres to internally accomplish the things.

The pg_namespace is a catalog, which is used to store the namespaces. And a namespace is the structure of fundamental SQL schemas, and each namespace can have a distinct collection of types, relations, etc. without any name conflicts.

Output

After executing the above query, we will get the below output, which contains one table, and we can see that the Schema1 schema is maintained by the owner id 10, which is Postgres id.

PostgreSQL ALTER Schema




Latest Courses