1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CREATE SCHEMA</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql-commands.html" title="SQL Commands">
<link rel="prev" href="sql-createrule.html" title="CREATE RULE">
<link rel="next" href="sql-createsequence.html" title="CREATE SEQUENCE">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
<a name="sql-createschema"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>CREATE SCHEMA — define a new schema</p>
</div>
<a name="id760983"></a><div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">CREATE SCHEMA <em class="replaceable"><code>schemaname</code></em> [ AUTHORIZATION <em class="replaceable"><code>username</code></em> ] [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]
CREATE SCHEMA AUTHORIZATION <em class="replaceable"><code>username</code></em> [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]</pre>
</div>
<div class="refsect1" lang="en">
<a name="id761034"></a><h2>Description</h2>
<p> <code class="command">CREATE SCHEMA</code> 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.
</p>
<p> A schema is essentially a namespace:
it contains named objects (tables, data types, functions, and operators)
whose names may duplicate those of other objects existing in other
schemas. Named objects are accessed either by “<span class="quote">qualifying</span>”
their names with the schema name as a prefix, or by setting a search
path that includes the desired schema(s). A <code class="literal">CREATE</code> 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 <code class="function">current_schema</code>).
</p>
<p> Optionally, <code class="command">CREATE SCHEMA</code> 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 <code class="literal">AUTHORIZATION</code> clause is used,
all the created objects will be owned by that user.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id761100"></a><h2>Parameters</h2>
<div class="variablelist"><dl>
<dt><span class="term"><em class="replaceable"><code>schemaname</code></em></span></dt>
<dd><p> The name of a schema to be created. If this is omitted, the user name
is used as the schema name. The name cannot
begin with <code class="literal">pg_</code>, as such names
are reserved for system schemas.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>username</code></em></span></dt>
<dd><p> The name of the user who will own the schema. If omitted,
defaults to the user executing the command. Only superusers
may create schemas owned by users other than themselves.
</p></dd>
<dt><span class="term"><em class="replaceable"><code>schema_element</code></em></span></dt>
<dd><p> An SQL statement defining an object to be created within the
schema. Currently, only <code class="command">CREATE
TABLE</code>, <code class="command">CREATE VIEW</code>, <code class="command">CREATE
INDEX</code>, <code class="command">CREATE SEQUENCE</code>, <code class="command">CREATE
TRIGGER</code> and <code class="command">GRANT</code> are accepted as clauses
within <code class="command">CREATE SCHEMA</code>. Other kinds of objects may
be created in separate commands after the schema is created.
</p></dd>
</dl></div>
</div>
<div class="refsect1" lang="en">
<a name="id761203"></a><h2>Notes</h2>
<p> To create a schema, the invoking user must have the
<code class="literal">CREATE</code> privilege for the current database.
(Of course, superusers bypass this check.)
</p>
</div>
<div class="refsect1" lang="en">
<a name="id761218"></a><h2>Examples</h2>
<p> Create a schema:
</p>
<pre class="programlisting">CREATE SCHEMA myschema;</pre>
<p>
</p>
<p> Create a schema for user <code class="literal">joe</code>; the schema will also be
named <code class="literal">joe</code>:
</p>
<pre class="programlisting">CREATE SCHEMA AUTHORIZATION joe;</pre>
<p>
</p>
<p> Create a schema and create a table and view within it:
</p>
<pre class="programlisting">CREATE SCHEMA hollywood
CREATE TABLE films (title text, release date, awards text[])
CREATE VIEW winners AS
SELECT title, release FROM films WHERE awards IS NOT NULL;</pre>
<p>
Notice that the individual subcommands do not end with semicolons.
</p>
<p> The following is an equivalent way of accomplishing the same result:
</p>
<pre class="programlisting">CREATE SCHEMA hollywood;
CREATE TABLE hollywood.films (title text, release date, awards text[]);
CREATE VIEW hollywood.winners AS
SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;</pre>
<p>
</p>
</div>
<div class="refsect1" lang="en">
<a name="id761282"></a><h2>Compatibility</h2>
<p> The SQL standard allows a <code class="literal">DEFAULT CHARACTER SET</code> clause
in <code class="command">CREATE SCHEMA</code>, as well as more subcommand
types than are presently accepted by
<span class="productname">PostgreSQL</span>.
</p>
<p> The SQL standard specifies that the subcommands in <code class="command">CREATE
SCHEMA</code> may appear in any order. The present
<span class="productname">PostgreSQL</span> implementation does not
handle all cases of forward references in subcommands; it may
sometimes be necessary to reorder the subcommands in order to avoid
forward references.
</p>
<p> According to the SQL standard, the owner of a schema always owns
all objects within it. <span class="productname">PostgreSQL</span>
allows schemas to contain objects owned by users other than the
schema owner. This can happen only if the schema owner grants the
<code class="literal">CREATE</code> privilege on his schema to someone else.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id761352"></a><h2>See Also</h2>
<span class="simplelist"><a href="sql-alterschema.html">ALTER SCHEMA</a>, <a href="sql-dropschema.html">DROP SCHEMA</a></span>
</div>
</div></body>
</html>
|