]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_SCHEMA.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_SCHEMA.7
1 '\" t
2 .\"     Title: CREATE SCHEMA
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE SCHEMA" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_SCHEMA \- define a new schema
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE SCHEMA \fIschema_name\fR [ AUTHORIZATION \fIrole_specification\fR ] [ \fIschema_element\fR [ \&.\&.\&. ] ]
36 CREATE SCHEMA AUTHORIZATION \fIrole_specification\fR [ \fIschema_element\fR [ \&.\&.\&. ] ]
37 CREATE SCHEMA IF NOT EXISTS \fIschema_name\fR [ AUTHORIZATION \fIrole_specification\fR ]
38 CREATE SCHEMA IF NOT EXISTS AUTHORIZATION \fIrole_specification\fR
39
40 where \fIrole_specification\fR can be:
41
42     \fIuser_name\fR
43   | CURRENT_ROLE
44   | CURRENT_USER
45   | SESSION_USER
46 .fi
47 .SH "DESCRIPTION"
48 .PP
49 \fBCREATE SCHEMA\fR
50 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\&.
51 .PP
52 A schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can duplicate those of other objects existing in other schemas\&. Named objects are accessed either by
53 \(lqqualifying\(rq
54 their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s)\&. A
55 CREATE
56 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
57 \fBcurrent_schema\fR)\&.
58 .PP
59 Optionally,
60 \fBCREATE SCHEMA\fR
61 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
62 AUTHORIZATION
63 clause is used, all the created objects will be owned by that user\&.
64 .SH "PARAMETERS"
65 .PP
66 \fIschema_name\fR
67 .RS 4
68 The name of a schema to be created\&. If this is omitted, the
69 \fIuser_name\fR
70 is used as the schema name\&. The name cannot begin with
71 pg_, as such names are reserved for system schemas\&.
72 .RE
73 .PP
74 \fIuser_name\fR
75 .RS 4
76 The role name of the user who will own the new schema\&. If omitted, defaults to the user executing the command\&. To create a schema owned by another role, you must be able to
77 SET ROLE
78 to that role\&.
79 .RE
80 .PP
81 \fIschema_element\fR
82 .RS 4
83 An SQL statement defining an object to be created within the schema\&. Currently, only
84 \fBCREATE TABLE\fR,
85 \fBCREATE VIEW\fR,
86 \fBCREATE INDEX\fR,
87 \fBCREATE SEQUENCE\fR,
88 \fBCREATE TRIGGER\fR
89 and
90 \fBGRANT\fR
91 are accepted as clauses within
92 \fBCREATE SCHEMA\fR\&. Other kinds of objects may be created in separate commands after the schema is created\&.
93 .RE
94 .PP
95 IF NOT EXISTS
96 .RS 4
97 Do nothing (except issuing a notice) if a schema with the same name already exists\&.
98 \fIschema_element\fR
99 subcommands cannot be included when this option is used\&.
100 .RE
101 .SH "NOTES"
102 .PP
103 To create a schema, the invoking user must have the
104 CREATE
105 privilege for the current database\&. (Of course, superusers bypass this check\&.)
106 .SH "EXAMPLES"
107 .PP
108 Create a schema:
109 .sp
110 .if n \{\
111 .RS 4
112 .\}
113 .nf
114 CREATE SCHEMA myschema;
115 .fi
116 .if n \{\
117 .RE
118 .\}
119 .PP
120 Create a schema for user
121 joe; the schema will also be named
122 joe:
123 .sp
124 .if n \{\
125 .RS 4
126 .\}
127 .nf
128 CREATE SCHEMA AUTHORIZATION joe;
129 .fi
130 .if n \{\
131 .RE
132 .\}
133 .PP
134 Create a schema named
135 test
136 that will be owned by user
137 joe, unless there already is a schema named
138 test\&. (It does not matter whether
139 joe
140 owns the pre\-existing schema\&.)
141 .sp
142 .if n \{\
143 .RS 4
144 .\}
145 .nf
146 CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
147 .fi
148 .if n \{\
149 .RE
150 .\}
151 .PP
152 Create a schema and create a table and view within it:
153 .sp
154 .if n \{\
155 .RS 4
156 .\}
157 .nf
158 CREATE SCHEMA hollywood
159     CREATE TABLE films (title text, release date, awards text[])
160     CREATE VIEW winners AS
161         SELECT title, release FROM films WHERE awards IS NOT NULL;
162 .fi
163 .if n \{\
164 .RE
165 .\}
166 .sp
167 Notice that the individual subcommands do not end with semicolons\&.
168 .PP
169 The following is an equivalent way of accomplishing the same result:
170 .sp
171 .if n \{\
172 .RS 4
173 .\}
174 .nf
175 CREATE SCHEMA hollywood;
176 CREATE TABLE hollywood\&.films (title text, release date, awards text[]);
177 CREATE VIEW hollywood\&.winners AS
178     SELECT title, release FROM hollywood\&.films WHERE awards IS NOT NULL;
179 .fi
180 .if n \{\
181 .RE
182 .\}
183 .SH "COMPATIBILITY"
184 .PP
185 The SQL standard allows a
186 DEFAULT CHARACTER SET
187 clause in
188 \fBCREATE SCHEMA\fR, as well as more subcommand types than are presently accepted by
189 PostgreSQL\&.
190 .PP
191 The SQL standard specifies that the subcommands in
192 \fBCREATE SCHEMA\fR
193 can appear in any order\&. The present
194 PostgreSQL
195 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\&.
196 .PP
197 According to the SQL standard, the owner of a schema always owns all objects within it\&.
198 PostgreSQL
199 allows schemas to contain objects owned by users other than the schema owner\&. This can happen only if the schema owner grants the
200 CREATE
201 privilege on their schema to someone else, or a superuser chooses to create objects in it\&.
202 .PP
203 The
204 IF NOT EXISTS
205 option is a
206 PostgreSQL
207 extension\&.
208 .SH "SEE ALSO"
209 ALTER SCHEMA (\fBALTER_SCHEMA\fR(7)), DROP SCHEMA (\fBDROP_SCHEMA\fR(7))