Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

CREATE SCHEMA

Updated on 2024-06-03 GMT+08:00

Description

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. 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.

Precautions

  • Only a user with the CREATE permission on the current database can perform this operation.
  • 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.

Syntax

  • Create a schema based on a specified name.
    CREATE SCHEMA schema_name 
        [ AUTHORIZATION user_name ] [WITH BLOCKCHAIN] [ schema_element [ ... ] ];

  • Create a schema based on a username.
    CREATE SCHEMA AUTHORIZATION user_name [ schema_element [ ... ] ];

  • Create a schema and specify the default character set and collation.
    CREATE SCHEMA schema_name 
        [ [DEFAULT] CHARACTER SET | CHARSET [ = ] default_charset ] [ [ DEFAULT] COLLATE [ = ] default_collation ];

Parameters

  • schema_name

    Specifies the schema name.

    NOTICE:
    • 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.

  • AUTHORIZATION user_name

    Specifies the owner of a 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 username or role name

  • WITH BLOCKCHAIN

    Specifies the tamper-proof attribute of a schema. In this mode, a row-store common user table is automatically extended to tamper-proof user table.

    NOTE:

    To create a tamper-proof schema, set the GUC parameter enable_ledger to on. The default value is off, and the level is SIGHUP.

  • schema_element

    Specifies an SQL statement defining an object to be created within the schema. Currently, only the CREATE TABLE, CREATE VIEW, CREATE INDEX, CREATE TABLE PARTITION, CREATE SEQUENCE, CREATE TRIGGER, and GRANT clauses are supported.

    Objects created by sub-commands are owned by the user specified by AUTHORIZATION.

  • default_charset

    Specifies the default character set of a schema. If this parameter is specified separately, the default collation of the schema is set to the default collation of the specified character set.

    This syntax is supported only when sql_compatibility is set to 'B'.

  • default_collation

    Specifies the default collation of a schema. If this parameter is specified separately, the default character set of the schema is set to the character set corresponding to the specified collation.

    This syntax is supported only when sql_compatibility is set to 'B'. For details about the supported collation, see Table 1.

NOTE:

If objects in the schema on the current search path are with the same name, specify the schemas for different objects. You can run SHOW SEARCH_PATH to check the schemas on the current search path.

Examples

-- Create and switch to the test database.
gaussdb=# CREATE DATABASE test1 WITH DBCOMPATIBILITY = 'B' ENCODING = 'UTF8' LC_COLLATE = 'zh_CN.utf8' LC_CTYPE = 'zh_CN.utf8';
gaussdb=# \c test1

-- Create the role1 role.
test1=# CREATE ROLE role1 IDENTIFIED BY '********';

-- Create a schema named role1 for the role1 role. The owner of the films and winners tables created by the clause is role1.
test1=# 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;

-- Create a schema ds and set the default character set of the schema to utf8mb4 and the default collation to utf8mb4_bin. This syntax is supported only when sql_compatibility is set to 'B'.
test1=# CREATE SCHEMA ds CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

-- Delete the schema.
test1=# DROP SCHEMA role1 CASCADE;
test1=# DROP SCHEMA ds CASCADE;

-- Delete the user.
test1=# DROP USER role1 CASCADE;

-- Switch back to the initial database and delete the test database. Replace postgres with the actual database name.
test1=# \c postgres
gaussdb=# DROP DATABASE test1;

Helpful Links

ALTER SCHEMA and DROP SCHEMA

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback