Esta página ainda não está disponível no idioma selecionado. Estamos trabalhando para adicionar mais opções de idiomas. Agradecemos sua compreensão.

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

Creating and Managing GaussDB(DWS) Schemas

Updated on 2024-12-18 GMT+08:00

A schema is the logical organization of objects and data in a database. Schema management allows multiple users to use the same database without interfering with each other. Third-party applications can be added to corresponding schemas to avoid conflicts.

The same database object name can be used in different schemas in a database without causing conflicts. For example, both a_schema and b_schema can contain a table named mytable. Users with required permissions can access objects across multiple schemas in a database.

If a user is created, a schema named after the user will also be created in the current database.

Public mode

Each database has a schema named public. All users have the ability to use the public schema in the database, but only certain roles have the authority to create objects within it.

Creating a Schema

  • Run the CREATE SCHEMA command to create a schema.
    1
    CREATE SCHEMA myschema;
    

    To create or access an object in the schema, the object name in the command should be composed of the schema name and the object name, which are separated by a dot (.), for example, myschema.table.

  • Users can create a schema owned by others. For example, run the following command to create a schema named myschema and set the owner of the schema to user jack:
    1
    CREATE SCHEMA myschema AUTHORIZATION jack;
    

    If authorization username is not specified, the schema owner is the user who runs the command.

Modifying a Schema

  • Run the ALTER SCHEMA command to change the schema name. Only the schema owner can change the schema name.
    1
    ALTER SCHEMA schema_name RENAME TO new_name;
    
  • Run the ALTER SCHEMA command to change the schema owner.
    1
    ALTER SCHEMA schema_name OWNER TO new_owner;
    

Setting the Schema Search Path

The GUC parameter search_path specifies the schema search sequence. The parameter value is a series of schema names separated by commas (,). If no schema is specified during object creation, the object will be added to the first schema displayed in the search path. If there are objects with the same name in different schemas and no schema is specified for an object query, the object will be returned from the first schema containing the object in the search path.

  • Run the SHOW command to view the current search path.
    1
    2
    3
    4
    5
    SHOW SEARCH_PATH;
     search_path
    ----------------
     "$user",public
    (1 row)
    

    The default value of search_path is "$user",public. $user indicates the name of the schema with the same name as the current session user. If the schema does not exist, $user will be ignored. By default, after a user connects to a database that has schemas with the same name, objects will be added to all the schemas. If there are no such schemas, objects will be added to only to the public schema.

  • Run the SET command to modify the default schema of the current session. For example, if the search path is set to "myschema, public", myschema is searched first.
    1
    SET SEARCH_PATH TO myschema, public;
    
    You can also run the ALTER ROLE command to set search_path for a role (user). For example:
    1
    ALTER ROLE jack SET search_path TO myschema, public;
    

Using a Schema

If you want to create or access an object in a specified schema, the object name must contain the schema name. To be specific, the name consists of a schema name and an object name, which are separated by a dot (.).

  • Create a table mytable in myschema. Create a table in schema_name.table_name format.
    1
    CREATE TABLE myschema.mytable(id int, name varchar(20));
    
  • Query all data in the table mytable in myschema.
    1
    2
    3
    4
    SELECT * FROM myschema.mytable;
     id | name
    ----+------
    (0 rows)
    

Viewing a Schema

  • Use the current_schema() function to view the current schema.
    1
    2
    3
    4
    5
    SELECT current_schema();
     current_schema 
    ----------------
     myschema
    (1 row)
    
  • To view the owner of a schema, perform the following join query on the system catalogs PG_NAMESPACE and PG_USER. Replace schema_name in the statement with the name of the schema to be queried.
    1
    SELECT s.nspname,u.usename AS nspowner FROM PG_NAMESPACE s, PG_USER u WHERE nspname='schema_name' AND s.nspowner = u.usesysid;
    
  • To view a list of all schemas, query the system catalog PG_NAMESPACE.
    1
    SELECT * FROM PG_NAMESPACE;
    
  • Use the PGXC_TOTAL_SCHEMA_INFO view to query the space usage of schemas in the cluster.
    1
    SELECT * FROM PGXC_TOTAL_SCHEMA_INFO;
    
  • To view a list of tables in a schema, query the system catalog PG_TABLES. For example, the following query will return a table list from PG_CATALOG in the schema.
    1
    SELECT distinct(tablename),schemaname FROM PG_TABLES where schemaname = 'pg_catalog';
    

Schema Permission Control

By default, a user can only access database objects in its own schema. To access objects in other schemas, the user must have the usage permission of the corresponding schema.

By granting the CREATE permission for a schema to a user, the user can create objects in this schema.

  • Grant the usage permission of myschema to user jack.
    1
    GRANT USAGE ON schema myschema TO jack;
    
  • Run the following command to revoke the USAGE permission for myschema from jack:
    1
    REVOKE USAGE ON schema myschema FROM jack;
    

Drop Schema

  • Run the DROP SCHEMA command to delete an empty schema (no database objects in the schema).
    1
    DROP SCHEMA IF EXISTS myschema;
    
  • By default, a schema must be empty before being deleted. To delete a schema and all its objects (such as tables, data, and functions), use the CASCADE keyword.
    1
    DROP SCHEMA myschema CASCADE;
    

System Schema

  • Each database has a pg_catalog schema, which contains system catalogs and all built-in data types, functions, and operators. pg_catalog is a part of the search path and has the second highest search priority. It is searched after the schema of temporary tables and before other schemas specified in search_path. This search order ensures that database built-in objects can be found. To use a custom object that has the same name as a built-in object, you can specify the schema of the custom object.
  • The information_schema consists of a collection of views that contain object information in a database. These views obtain system information from the system catalogs in a standardized way.

Usamos cookies para aprimorar nosso site e sua experiência. Ao continuar a navegar em nosso site, você aceita nossa política de cookies. Saiba mais

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback