2 .\" Title: CREATE SCHEMA
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE SCHEMA" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_SCHEMA \- define a new schema
35 CREATE SCHEMA \fIschema_name\fR [ AUTHORIZATION \fIrole_specification\fR ] [ \fIschema_element\fR [ \&.\&.\&. ] ]
36 CREATE SCHEMA AUTHORIZATION \fIrole_specification\fR [ \fIschema_element\fR [ \&.\&.\&. ] ]
37 CREATE SCHEMA IF NOT EXISTS \fIschema_name\fR [ AUTHORIZATION \fIrole_specification\fR ]
38 CREATE SCHEMA IF NOT EXISTS AUTHORIZATION \fIrole_specification\fR
40 where \fIrole_specification\fR can be:
50 enters a new schema into the current database\&. The schema name must be distinct from the name of any existing schema in the current database\&.
52 A schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can duplicate those of other objects existing in other schemas\&. Named objects are accessed either by
54 their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s)\&. A
56 command specifying an unqualified object name creates the object in the current schema (the one at the front of the search path, which can be determined with the function
57 \fBcurrent_schema\fR)\&.
61 can include subcommands to create objects within the new schema\&. The subcommands are treated essentially the same as separate commands issued after creating the schema, except that if the
63 clause is used, all the created objects will be owned by that user\&.
68 The name of a schema to be created\&. If this is omitted, the
70 is used as the schema name\&. The name cannot begin with
71 pg_, as such names are reserved for system schemas\&.
76 The role name of the user who will own the new schema\&. If omitted, defaults to the user executing the command\&. To create a schema owned by another role, you must be able to
83 An SQL statement defining an object to be created within the schema\&. Currently, only
87 \fBCREATE SEQUENCE\fR,
91 are accepted as clauses within
92 \fBCREATE SCHEMA\fR\&. Other kinds of objects may be created in separate commands after the schema is created\&.
97 Do nothing (except issuing a notice) if a schema with the same name already exists\&.
99 subcommands cannot be included when this option is used\&.
103 To create a schema, the invoking user must have the
105 privilege for the current database\&. (Of course, superusers bypass this check\&.)
114 CREATE SCHEMA myschema;
120 Create a schema for user
121 joe; the schema will also be named
128 CREATE SCHEMA AUTHORIZATION joe;
134 Create a schema named
136 that will be owned by user
137 joe, unless there already is a schema named
138 test\&. (It does not matter whether
140 owns the pre\-existing schema\&.)
146 CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
152 Create a schema and create a table and view within it:
158 CREATE SCHEMA hollywood
159 CREATE TABLE films (title text, release date, awards text[])
160 CREATE VIEW winners AS
161 SELECT title, release FROM films WHERE awards IS NOT NULL;
167 Notice that the individual subcommands do not end with semicolons\&.
169 The following is an equivalent way of accomplishing the same result:
175 CREATE SCHEMA hollywood;
176 CREATE TABLE hollywood\&.films (title text, release date, awards text[]);
177 CREATE VIEW hollywood\&.winners AS
178 SELECT title, release FROM hollywood\&.films WHERE awards IS NOT NULL;
185 The SQL standard allows a
186 DEFAULT CHARACTER SET
188 \fBCREATE SCHEMA\fR, as well as more subcommand types than are presently accepted by
191 The SQL standard specifies that the subcommands in
193 can appear in any order\&. The present
195 implementation does not handle all cases of forward references in subcommands; it might sometimes be necessary to reorder the subcommands in order to avoid forward references\&.
197 According to the SQL standard, the owner of a schema always owns all objects within it\&.
199 allows schemas to contain objects owned by users other than the schema owner\&. This can happen only if the schema owner grants the
201 privilege on their schema to someone else, or a superuser chooses to create objects in it\&.
209 ALTER SCHEMA (\fBALTER_SCHEMA\fR(7)), DROP SCHEMA (\fBDROP_SCHEMA\fR(7))