CREATE SCHEMA
Description
Creates a schema. DATABASE is synonymous with 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.
- templatem cannot be used as the schema name when creating a schema.
Syntax
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] database_name [AUTHORIZATIO role_id]
[[create_option] [,...]]
create_option: [DEFAULT] {
CHARACTER SET [=] default_charset
| CHAR SET [=] default_charset
| CHARSET [=] default_charset
| COLLATE [=] default_collation
}
Parameters
- schema_name
Specifies the schema name.
- 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 complying with Identifier Description.
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.
- AUTHORIZATION role_id
(Optional) Speicfies the owner of the created database. By default, the owner of the database is the current user.
Value range: an existing username.
- COLLATE [=] collation_name
(Optional) Specifies the character set used by the new database. For example, set this parameter by using collate = 'zh_CN.gbk'.
The use of this parameter affects the sort order of strings (for example, the order of using ORDER BY for execution and the order of using indexes on text columns). By default, the character set of the template database is used.
Value range: See Database-level Character Sets and Collations.
- {CHAR SET | CHARSET | CHARACTER SET} [=] charset_name
(Optional) Specifies the character classification used by the new database. For example, set this parameter by using CHARSET = 'zh_CN.gbk'. The use of this parameter affects the classification of characters, such as uppercase letters, lowercase letters, and digits. By default, the character classification of the template database is used.
Value range: See Database-level Character Sets and Collations.
Examples
-- Create and switch to the test database. m_db=# CREATE SCHEMA test1; m_db=# USE test1; -- Create the a table. m_db=# CREATE TABLE a(id int); m_db=# DROP TABLE a; -- Drop the test1 schema. m_db=# USE public; m_db=# DROP SCHEMA test1;
Helpful Links
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot