CREATE SCHEMA creates a schema.
Named objects are accessed either by "qualifying" their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s). When creating named objects, you can also use the schema name as a prefix.
Optionally, CREATE SCHEMA can include sub-commands to create objects within the new schema. The sub-commands are treated essentially the same as separate commands issued after creating the schema, If the AUTHORIZATION clause is used, all the created objects are owned by this user.
- As long as the current user has the CREATE permission, the user can create a schema.
- The owner of an object created by a system administrator in a schema with the same name as a common user is the common user, not the system administrator.
- Create a schema based on a specified name.
CREATE SCHEMA schema_name [ AUTHORIZATION user_name ] [ schema_element [ ... ] ];
- Create a schema based on a user name.
CREATE SCHEMA AUTHORIZATION user_name [ schema_element [ ... ] ];
Indicates the name of the schema to be created.
The name must be unique,
and cannot start with pg_.
Value range: a string. It must comply with the naming convention rule.
- AUTHORIZATION user_name
Indicates the name of the user who will own this schema. If schema_name is not specified, user_name will be used as the schema name. In this case, user_name can only be a role name.
Value range: An existing user name/role.
Indicates an SQL statement defining an object to be created within the schema. Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX, CREATE PARTITION, and GRANT are accepted as clauses within CREATE SCHEMA.
Objects created by sub-commands are owned by the user specified by AUTHORIZATION.
If objects in the schema on the current search path are with the same name, specify the schemas different objects are in. You can run the SHOW SEARCH_PATH command to check the schemas on the current search path.
1 2 3 4 5 6 7 8 9 10 11 12 13
-- Create the role1 role: CREATE ROLE role1 IDENTIFIED BY 'Bigdata123@'; -- Create a schema named role1 for the role1 role. The owner of the films and winners tables created by the clause is role1. CREATE SCHEMA AUTHORIZATION role1 CREATE TABLE films (title text, release date, awards text) CREATE VIEW winners AS SELECT title, release FROM films WHERE awards IS NOT NULL; -- Delete the schema: DROP SCHEMA role1 CASCADE; -- Delete the user: DROP USER role1 CASCADE;