codekingpro/portable-devtools
115k
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 16.3 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">3CREATE 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> [ ... ] ]4CREATE SCHEMA AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> [ <em class="replaceable"><code>schema_element</code></em> [ ... ] ]5CREATE SCHEMA IF NOT EXISTS <em class="replaceable"><code>schema_name</code></em> [ AUTHORIZATION <em class="replaceable"><code>role_specification</code></em> ]6CREATE 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_ROLE12 | CURRENT_USER13 | SESSION_USER14</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 schema16 into the current database.17 The schema name must be distinct from the name of any existing schema18 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 other23 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 search25 path that includes the desired schema(s). A <code class="literal">CREATE</code> command26 specifying an unqualified object name creates the object27 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 subcommands31 to create objects within the new schema. The subcommands are treated32 essentially the same as separate commands issued after creating the33 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, the37 <em class="replaceable"><code>user_name</code></em>38 is used as the schema name. The name cannot39 begin with <code class="literal">pg_</code>, as such names40 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 schema44 owned by another role, you must be able to45 <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 the48 schema. Currently, only <code class="command">CREATE49 TABLE</code>, <code class="command">CREATE VIEW</code>, <code class="command">CREATE50 INDEX</code>, <code class="command">CREATE SEQUENCE</code>, <code class="command">CREATE51 TRIGGER</code> and <code class="command">GRANT</code> are accepted as clauses52 within <code class="command">CREATE SCHEMA</code>. Other kinds of objects may53 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 name56 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 the60 <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">65CREATE SCHEMA myschema;66</pre><p>67 </p><p>68 Create a schema for user <code class="literal">joe</code>; the schema will also be69 named <code class="literal">joe</code>:70</p><pre class="programlisting">71CREATE SCHEMA AUTHORIZATION joe;72</pre><p>73 </p><p>74 Create a schema named <code class="literal">test</code> that will be owned by user75 <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">78CREATE 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">83CREATE SCHEMA hollywood84 CREATE TABLE films (title text, release date, awards text[])85 CREATE VIEW winners AS86 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">92CREATE SCHEMA hollywood;93CREATE TABLE hollywood.films (title text, release date, awards text[]);94CREATE VIEW hollywood.winners AS95 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> clause98 in <code class="command">CREATE SCHEMA</code>, as well as more subcommand99 types than are presently accepted by100 <span class="productname">PostgreSQL</span>.101 </p><p>102 The SQL standard specifies that the subcommands in <code class="command">CREATE103 SCHEMA</code> can appear in any order. The present104 <span class="productname">PostgreSQL</span> implementation does not105 handle all cases of forward references in subcommands; it might106 sometimes be necessary to reorder the subcommands in order to avoid107 forward references.108 </p><p>109 According to the SQL standard, the owner of a schema always owns110 all objects within it. <span class="productname">PostgreSQL</span>111 allows schemas to contain objects owned by users other than the112 schema owner. This can happen only if the schema owner grants the113 <code class="literal">CREATE</code> privilege on their schema to someone else, or a114 superuser chooses to create objects in it.115 </p><p>116 The <code class="literal">IF NOT EXISTS</code> option is a117 <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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE SEQUENCE</td></tr></table></div></body></html>