]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_schema.sgml
Fix psql doc typo.
[postgresql] / doc / src / sgml / ref / create_schema.sgml
1 <!--
2 doc/src/sgml/ref/create_schema.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATESCHEMA">
7  <refmeta>
8   <refentrytitle>CREATE SCHEMA</refentrytitle>
9   <manvolnum>7</manvolnum>
10   <refmiscinfo>SQL - Language Statements</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>CREATE SCHEMA</refname>
15   <refpurpose>define a new schema</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="sql-createschema">
19   <primary>CREATE SCHEMA</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23 <synopsis>
24 CREATE SCHEMA <replaceable class="parameter">schema_name</replaceable> [ AUTHORIZATION <replaceable class="parameter">user_name</replaceable> ] [ <replaceable class="parameter">schema_element</replaceable> [ ... ] ]
25 CREATE SCHEMA AUTHORIZATION <replaceable class="parameter">user_name</replaceable> [ <replaceable class="parameter">schema_element</replaceable> [ ... ] ]
26 </synopsis>
27  </refsynopsisdiv>
28
29  <refsect1>
30   <title>Description</title>
31
32   <para>
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.
37   </para>
38
39   <para>
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>).
49   </para>
50
51   <para>
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.
57   </para>
58  </refsect1>
59
60  <refsect1>
61   <title>Parameters</title>
62
63     <variablelist>
64      <varlistentry>
65       <term><replaceable class="parameter">schema_name</replaceable></term>
66       <listitem>
67        <para>
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.
72        </para>
73       </listitem>
74      </varlistentry>
75
76      <varlistentry>
77       <term><replaceable class="parameter">user_name</replaceable></term>
78       <listitem>
79        <para>
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.
83        </para>
84       </listitem>
85      </varlistentry>
86
87      <varlistentry>
88       <term><replaceable class="parameter">schema_element</replaceable></term>
89       <listitem>
90        <para>
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.
98        </para>
99       </listitem>
100      </varlistentry>
101     </variablelist>
102  </refsect1>
103
104  <refsect1>
105   <title>Notes</title>
106
107   <para>
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.)
111   </para>
112  </refsect1>
113
114  <refsect1>
115   <title>Examples</title>
116
117   <para>
118    Create a schema:
119 <programlisting>
120 CREATE SCHEMA myschema;
121 </programlisting>
122   </para>
123
124   <para>
125    Create a schema for user <literal>joe</>; the schema will also be
126    named <literal>joe</>:
127 <programlisting>
128 CREATE SCHEMA AUTHORIZATION joe;
129 </programlisting>
130   </para>
131
132   <para>
133    Create a schema and create a table and view within it:
134 <programlisting>
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;
139 </programlisting>
140    Notice that the individual subcommands do not end with semicolons.
141   </para>
142
143   <para>
144    The following is an equivalent way of accomplishing the same result:
145 <programlisting>
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;
150 </programlisting></para>
151
152  </refsect1>
153
154  <refsect1>
155   <title>Compatibility</title>
156
157   <para>
158    The SQL standard allows a <literal>DEFAULT CHARACTER SET</> clause
159    in <command>CREATE SCHEMA</command>, as well as more subcommand
160    types than are presently accepted by
161    <productname>PostgreSQL</productname>.
162   </para>
163
164   <para>
165    The SQL standard specifies that the subcommands in <command>CREATE
166    SCHEMA</command> can appear in any order.  The present
167    <productname>PostgreSQL</productname> implementation does not
168    handle all cases of forward references in subcommands; it might
169    sometimes be necessary to reorder the subcommands in order to avoid
170    forward references.
171   </para>
172
173   <para>
174    According to the SQL standard, the owner of a schema always owns
175    all objects within it.  <productname>PostgreSQL</productname>
176    allows schemas to contain objects owned by users other than the
177    schema owner.  This can happen only if the schema owner grants the
178    <literal>CREATE</> privilege on his schema to someone else.
179   </para>
180  </refsect1>
181
182  <refsect1>
183   <title>See Also</title>
184
185   <simplelist type="inline">
186    <member><xref linkend="sql-alterschema"></member>
187    <member><xref linkend="sql-dropschema"></member>
188  </simplelist>
189  </refsect1>
190
191 </refentry>