3 PostgreSQL documentation
6 <refentry id=
"SQL-CREATESCHEMA">
8 <refentrytitle id=
"sql-createschema-title">CREATE SCHEMA
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>CREATE SCHEMA
</refname>
15 <refpurpose>define a new schema
</refpurpose>
18 <indexterm zone=
"sql-createschema">
19 <primary>CREATE SCHEMA
</primary>
24 CREATE SCHEMA
<replaceable class=
"parameter">schemaname
</replaceable> [ AUTHORIZATION
<replaceable class=
"parameter">username
</replaceable> ] [
<replaceable class=
"parameter">schema_element
</replaceable> [ ... ] ]
25 CREATE SCHEMA AUTHORIZATION
<replaceable class=
"parameter">username
</replaceable> [
<replaceable class=
"parameter">schema_element
</replaceable> [ ... ] ]
30 <title>Description
</title>
33 <command>CREATE SCHEMA
</command> enters a new schema
34 into the current database.
35 The schema name must be distinct from the name of any existing schema
36 in the current database.
40 A schema is essentially a namespace:
41 it contains named objects (tables, data types, functions, and operators)
42 whose names can duplicate those of other objects existing in other
43 schemas. Named objects are accessed either by
<quote>qualifying<
/>
44 their names with the schema name as a prefix, or by setting a search
45 path that includes the desired schema(s). A
<literal>CREATE<
/> command
46 specifying an unqualified object name creates the object
47 in the current schema (the one at the front of the search path,
48 which can be determined with the function
<function>current_schema
</function>).
52 Optionally,
<command>CREATE SCHEMA
</command> can include subcommands
53 to create objects within the new schema. The subcommands are treated
54 essentially the same as separate commands issued after creating the
55 schema, except that if the
<literal>AUTHORIZATION<
/> clause is used,
56 all the created objects will be owned by that user.
61 <title>Parameters
</title>
65 <term><replaceable class=
"parameter">schemaname
</replaceable></term>
68 The name of a schema to be created. If this is omitted, the user name
69 is used as the schema name. The name cannot
70 begin with
<literal>pg_
</literal>, as such names
71 are reserved for system schemas.
77 <term><replaceable class=
"parameter">username
</replaceable></term>
80 The name of the user who will own the schema. If omitted,
81 defaults to the user executing the command. Only superusers
82 can create schemas owned by users other than themselves.
88 <term><replaceable class=
"parameter">schema_element
</replaceable></term>
91 An SQL statement defining an object to be created within the
92 schema. Currently, only
<command>CREATE
93 TABLE<
/>,
<command>CREATE VIEW<
/>,
<command>CREATE
94 INDEX<
/>,
<command>CREATE SEQUENCE<
/>,
<command>CREATE
95 TRIGGER<
/> and
<command>GRANT<
/> are accepted as clauses
96 within
<command>CREATE SCHEMA<
/>. Other kinds of objects may
97 be created in separate commands after the schema is created.
108 To create a schema, the invoking user must have the
109 <literal>CREATE<
/> privilege for the current database.
110 (Of course, superusers bypass this check.)
115 <title>Examples
</title>
120 CREATE SCHEMA myschema;
125 Create a schema for user
<literal>joe<
/>; the schema will also be
126 named
<literal>joe<
/>:
128 CREATE SCHEMA AUTHORIZATION joe;
133 Create a schema and create a table and view within it:
135 CREATE SCHEMA hollywood
136 CREATE TABLE films (title text, release date, awards text[])
137 CREATE VIEW winners AS
138 SELECT title, release FROM films WHERE awards IS NOT NULL;
140 Notice that the individual subcommands do not end with semicolons.
144 The following is an equivalent way of accomplishing the same result:
146 CREATE SCHEMA hollywood;
147 CREATE TABLE hollywood.films (title text, release date, awards text[]);
148 CREATE VIEW hollywood.winners AS
149 SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
156 <title>Compatibility
</title>
159 The SQL standard allows a
<literal>DEFAULT CHARACTER SET<
/> clause
160 in
<command>CREATE SCHEMA
</command>, as well as more subcommand
161 types than are presently accepted by
162 <productname>PostgreSQL
</productname>.
166 The SQL standard specifies that the subcommands in
<command>CREATE
167 SCHEMA
</command> can appear in any order. The present
168 <productname>PostgreSQL
</productname> implementation does not
169 handle all cases of forward references in subcommands; it might
170 sometimes be necessary to reorder the subcommands in order to avoid
175 According to the SQL standard, the owner of a schema always owns
176 all objects within it.
<productname>PostgreSQL
</productname>
177 allows schemas to contain objects owned by users other than the
178 schema owner. This can happen only if the schema owner grants the
179 <literal>CREATE<
/> privilege on his schema to someone else.
184 <title>See Also
</title>
186 <simplelist type=
"inline">
187 <member><xref linkend=
"sql-alterschema" endterm=
"sql-alterschema-title"></member>
188 <member><xref linkend=
"sql-dropschema" endterm=
"sql-dropschema-title"></member>