Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/ref/create_schema.sgml`
79c6d48222455f5faa7bbdb1cbcf3cb7273ebd56735ad0220000000100000d70
 NOT EXISTS</literal></term>
      <listitem>
       <para>
        Do nothing (except issuing a notice) if a schema with the same name
        already exists.  <replaceable class="parameter">schema_element</replaceable>
        subcommands cannot be included when this option is used.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   To create a schema, the invoking user must have the
   <literal>CREATE</literal> privilege for the current database.
   (Of course, superusers bypass this check.)
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Create a schema:
<programlisting>
CREATE SCHEMA myschema;
</programlisting>
  </para>

  <para>
   Create a schema for user <literal>joe</literal>; the schema will also be
   named <literal>joe</literal>:
<programlisting>
CREATE SCHEMA AUTHORIZATION joe;
</programlisting>
  </para>

  <para>
   Create a schema named <literal>test</literal> that will be owned by user
   <literal>joe</literal>, unless there already is a schema named <literal>test</literal>.
   (It does not matter whether <literal>joe</literal> owns the pre-existing schema.)
<programlisting>
CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
</programlisting>
  </para>

  <para>
   Create a schema and create a table and view within it:
<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;
</programlisting>
   Notice that the individual subcommands do not end with semicolons.
  </para>

  <para>
   The following is an equivalent way of accomplishing the same result:
<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;
</programlisting></para>

 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   The SQL standard allows a <literal>DEFAULT CHARACTER SET</literal> clause
   in <command>CREATE SCHEMA</command>, as well as more subcommand
   types than are presently accepted by
   <productname>PostgreSQL</productname>.
  </para>

  <para>
   The SQL standard specifies that the subcommands in <command>CREATE
   SCHEMA</command> can appear in any order.  The present
   <productname>PostgreSQL</productname> 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.
  </para>

  <para>
   According to the SQL standard, the owner of a schema always owns
   all objects within it.  <productname>PostgreSQL</productname>
   allows schemas to contain objects owned by users other than the
   schema owner.  This can happen only if the schema owner grants the
   <literal>CREATE</literal> privilege on their schema to someone else, or a
   superuser chooses to create objects in it.
  </para>

  <para>
   The <literal>IF NOT EXISTS</literal> option is a
   <productname>PostgreSQL</productname> extension.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-alterschema"/></member>
   <member><xref linkend="sql-dropschema"/></member>
 </simplelist>
 </refsect1>

</refentry>

Title: CREATE SCHEMA Examples, Compatibility, and See Also
Summary
The text provides various examples of using the CREATE SCHEMA command, including creating a schema for a specific user, creating a schema only if it doesn't exist, and creating tables and views within a schema. It also discusses the compatibility of CREATE SCHEMA with the SQL standard, noting differences in supported clauses, subcommand order, object ownership, and the IF NOT EXISTS option. Finally, it provides links to related commands like ALTER SCHEMA and DROP SCHEMA.