]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-createschema.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / sql-createschema.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE SCHEMA</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-createrule.html" title="CREATE RULE" /><link rel="next" href="sql-createsequence.html" title="CREATE SEQUENCE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE SCHEMA</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createrule.html" title="CREATE RULE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createsequence.html" title="CREATE SEQUENCE">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-CREATESCHEMA"><div class="titlepage"></div><a id="id-1.9.3.80.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE SCHEMA</span></h2><p>CREATE SCHEMA — define a new schema</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 CREATE SCHEMA <em class="replaceable"><code>schema_name</code></em> [ AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> ] [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]
4 CREATE SCHEMA AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]
5 CREATE SCHEMA IF NOT EXISTS <em class="replaceable"><code>schema_name</code></em> [ AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> ]
6 CREATE SCHEMA IF NOT EXISTS AUTHORIZATION <em class="replaceable"><code>role_specification</code></em>
7
8 <span class="phrase">where <em class="replaceable"><code>role_specification</code></em> can be:</span>
9
10     <em class="replaceable"><code>user_name</code></em>
11   | CURRENT_ROLE
12   | CURRENT_USER
13   | SESSION_USER
14 </pre></div><div class="refsect1" id="id-1.9.3.80.5"><h2>Description</h2><p>
15    <code class="command">CREATE SCHEMA</code> enters a new schema
16    into the current database.
17    The schema name must be distinct from the name of any existing schema
18    in the current database.
19   </p><p>
20    A schema is essentially a namespace:
21    it contains named objects (tables, data types, functions, and operators)
22    whose names can duplicate those of other objects existing in other
23    schemas.  Named objects are accessed either by <span class="quote">“<span class="quote">qualifying</span>”</span>
24    their names with the schema name as a prefix, or by setting a search
25    path that includes the desired schema(s).  A <code class="literal">CREATE</code> command
26    specifying an unqualified object name creates the object
27    in the current schema (the one at the front of the search path,
28    which can be determined with the function <code class="function">current_schema</code>).
29   </p><p>
30    Optionally, <code class="command">CREATE SCHEMA</code> can include subcommands
31    to create objects within the new schema.  The subcommands are treated
32    essentially the same as separate commands issued after creating the
33    schema, except that if the <code class="literal">AUTHORIZATION</code> clause is used,
34    all the created objects will be owned by that user.
35   </p></div><div class="refsect1" id="id-1.9.3.80.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>schema_name</code></em></span></dt><dd><p>
36         The name of a schema to be created.  If this is omitted, the
37         <em class="replaceable"><code>user_name</code></em>
38         is used as the schema name.  The name cannot
39         begin with <code class="literal">pg_</code>, as such names
40         are reserved for system schemas.
41        </p></dd><dt><span class="term"><em class="replaceable"><code>user_name</code></em></span></dt><dd><p>
42         The role name of the user who will own the new schema.  If omitted,
43         defaults to the user executing the command.  To create a schema
44         owned by another role, you must be able to
45         <code class="literal">SET ROLE</code> to that role.
46        </p></dd><dt><span class="term"><em class="replaceable"><code>schema_element</code></em></span></dt><dd><p>
47         An SQL statement defining an object to be created within the
48         schema. Currently, only <code class="command">CREATE
49         TABLE</code>, <code class="command">CREATE VIEW</code>, <code class="command">CREATE
50         INDEX</code>, <code class="command">CREATE SEQUENCE</code>, <code class="command">CREATE
51         TRIGGER</code> and <code class="command">GRANT</code> are accepted as clauses
52         within <code class="command">CREATE SCHEMA</code>. Other kinds of objects may
53         be created in separate commands after the schema is created.
54        </p></dd><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
55         Do nothing (except issuing a notice) if a schema with the same name
56         already exists.  <em class="replaceable"><code>schema_element</code></em>
57         subcommands cannot be included when this option is used.
58        </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.80.7"><h2>Notes</h2><p>
59    To create a schema, the invoking user must have the
60    <code class="literal">CREATE</code> privilege for the current database.
61    (Of course, superusers bypass this check.)
62   </p></div><div class="refsect1" id="id-1.9.3.80.8"><h2>Examples</h2><p>
63    Create a schema:
64 </p><pre class="programlisting">
65 CREATE SCHEMA myschema;
66 </pre><p>
67   </p><p>
68    Create a schema for user <code class="literal">joe</code>; the schema will also be
69    named <code class="literal">joe</code>:
70 </p><pre class="programlisting">
71 CREATE SCHEMA AUTHORIZATION joe;
72 </pre><p>
73   </p><p>
74    Create a schema named <code class="literal">test</code> that will be owned by user
75    <code class="literal">joe</code>, unless there already is a schema named <code class="literal">test</code>.
76    (It does not matter whether <code class="literal">joe</code> owns the pre-existing schema.)
77 </p><pre class="programlisting">
78 CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
79 </pre><p>
80   </p><p>
81    Create a schema and create a table and view within it:
82 </p><pre class="programlisting">
83 CREATE SCHEMA hollywood
84     CREATE TABLE films (title text, release date, awards text[])
85     CREATE VIEW winners AS
86         SELECT title, release FROM films WHERE awards IS NOT NULL;
87 </pre><p>
88    Notice that the individual subcommands do not end with semicolons.
89   </p><p>
90    The following is an equivalent way of accomplishing the same result:
91 </p><pre class="programlisting">
92 CREATE SCHEMA hollywood;
93 CREATE TABLE hollywood.films (title text, release date, awards text[]);
94 CREATE VIEW hollywood.winners AS
95     SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
96 </pre></div><div class="refsect1" id="id-1.9.3.80.9"><h2>Compatibility</h2><p>
97    The SQL standard allows a <code class="literal">DEFAULT CHARACTER SET</code> clause
98    in <code class="command">CREATE SCHEMA</code>, as well as more subcommand
99    types than are presently accepted by
100    <span class="productname">PostgreSQL</span>.
101   </p><p>
102    The SQL standard specifies that the subcommands in <code class="command">CREATE
103    SCHEMA</code> can appear in any order.  The present
104    <span class="productname">PostgreSQL</span> implementation does not
105    handle all cases of forward references in subcommands; it might
106    sometimes be necessary to reorder the subcommands in order to avoid
107    forward references.
108   </p><p>
109    According to the SQL standard, the owner of a schema always owns
110    all objects within it.  <span class="productname">PostgreSQL</span>
111    allows schemas to contain objects owned by users other than the
112    schema owner.  This can happen only if the schema owner grants the
113    <code class="literal">CREATE</code> privilege on their schema to someone else, or a
114    superuser chooses to create objects in it.
115   </p><p>
116    The <code class="literal">IF NOT EXISTS</code> option is a
117    <span class="productname">PostgreSQL</span> extension.
118   </p></div><div class="refsect1" id="id-1.9.3.80.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterschema.html" title="ALTER SCHEMA"><span class="refentrytitle">ALTER SCHEMA</span></a>, <a class="xref" href="sql-dropschema.html" title="DROP SCHEMA"><span class="refentrytitle">DROP SCHEMA</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createrule.html" title="CREATE RULE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createsequence.html" title="CREATE SEQUENCE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE RULE </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE SEQUENCE</td></tr></table></div></body></html>