Updated on 2024-05-07 GMT+08:00

ALTER SCHEMA

Description

Modifies schema attributes.

Precautions

  • Only the owner of a schema or users granted with the ALTER permission on the schema can run the ALTER SCHEMA command. The system administrator has this permission by default. To change the owner of a schema, you must be the owner of the schema or system administrator and a member of the new owner role.
  • For system schemas other than public, such as pg_catalog and sys, only the initial user is allowed to change the owner of a schema. Changing the names of the built-in system schemas may make some functions unavailable or even affect the normal running of the database. By default, the names of the built-in system schemas cannot be changed. To ensure forward compatibility, you can change the names of the built-in system schemas only when the system is being started or upgraded or when allow_system_table_mods is set to on.
  • Only the initial user can change the owner of a schema to an O&M administrator. Other users cannot change the owner of a schema to an O&M administrator.

Syntax

  • Rename a schema.
    1
    2
    ALTER SCHEMA schema_name 
        RENAME TO new_name;
    
  • Change the owner of a schema.
    1
    2
    ALTER SCHEMA schema_name 
        OWNER TO new_owner;
    

Parameters

  • schema_name

    Specifies the name of an existing schema.

    Value range: an existing schema name.

  • RENAME TO new_name

    Rename a schema.

    new_name: new name of the schema.

    • The schema name must be unique in the current database.
    • The schema name cannot be the same as the initial username of the current database.
    • The schema name cannot start with pg_.
    • The schema name cannot start with gs_role_.

    Value range: a string. It must comply with the naming convention.

  • OWNER TO new_owner

    Change the owner of a schema. To do this as a non-administrator, you must be a direct or indirect member of the new owner role, and that role must have the CREATE permission on the database.

    new_owner: new owner of the schema.

    Value range: an existing username or role name.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-- Create the ds schema.
gaussdb=# CREATE SCHEMA ds;

-- Rename the current schema ds to ds_new.
gaussdb=# ALTER SCHEMA ds RENAME TO ds_new;

-- Create user jack.
gaussdb=# CREATE USER jack PASSWORD '********';

-- Change the owner of ds_new to jack.
gaussdb=# ALTER SCHEMA ds_new OWNER TO jack;


-- Delete user jack and schema ds_new.
gaussdb=# DROP SCHEMA ds_new;
gaussdb=# DROP USER jack;

Helpful Links

CREATE SCHEMA and DROP SCHEMA