3
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4
<title>5.8.�Schemas</title>
5
<link rel="stylesheet" href="stylesheet.css" type="text/css">
6
<link rev="made" href="pgsql-docs@postgresql.org">
7
<meta name="generator" content="DocBook XSL Stylesheets V1.64.1">
8
<link rel="home" href="index.html" title="PostgreSQL 8.0.0beta5 Documentation">
9
<link rel="up" href="ddl.html" title="Chapter�5.�Data Definition">
10
<link rel="previous" href="ddl-priv.html" title="5.7.�Privileges">
11
<link rel="next" href="ddl-others.html" title="5.9.�Other Database Objects">
13
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
14
<div class="titlepage">
15
<div><div><h2 class="title" style="clear: both">
16
<a name="ddl-schemas"></a>5.8.�Schemas</h2></div></div>
19
<a name="id2525945"></a><p> A <span class="productname">PostgreSQL</span> database cluster
20
contains one or more named databases. Users and groups of users are
21
shared across the entire cluster, but no other data is shared across
22
databases. Any given client connection to the server can access
23
only the data in a single database, the one specified in the connection
26
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
27
<h3 class="title">Note</h3>
28
<p> Users of a cluster do not necessarily have the privilege to access every
29
database in the cluster. Sharing of user names means that there
30
cannot be different users named, say, <tt class="literal">joe</tt> in two databases
31
in the same cluster; but the system can be configured to allow
32
<tt class="literal">joe</tt> access to only some of the databases.
35
<p> A database contains one or more named <i class="firstterm">schemas</i>, which
36
in turn contain tables. Schemas also contain other kinds of named
37
objects, including data types, functions, and operators. The same
38
object name can be used in different schemas without conflict; for
39
example, both <tt class="literal">schema1</tt> and <tt class="literal">myschema</tt> may
40
contain tables named <tt class="literal">mytable</tt>. Unlike databases,
41
schemas are not rigidly separated: a user may access objects in any
42
of the schemas in the database he is connected to, if he has
45
<p> There are several reasons why one might want to use schemas:
48
<div class="itemizedlist"><ul type="disc">
49
<li><p> To allow many users to use one database without interfering with
52
<li><p> To organize database objects into logical groups to make them
55
<li><p> Third-party applications can be put into separate schemas so
56
they cannot collide with the names of other objects.
61
Schemas are analogous to directories at the operating system level,
62
except that schemas cannot be nested.
64
<div class="sect2" lang="en">
65
<div class="titlepage">
66
<div><div><h3 class="title">
67
<a name="ddl-schemas-create"></a>5.8.1.�Creating a Schema</h3></div></div>
70
<a name="id2526050"></a><p> To create a separate schema, use the command <tt class="literal">CREATE
71
SCHEMA</tt>. Give the schema a name of your choice. For
74
<pre class="programlisting">CREATE SCHEMA myschema;</pre>
77
<a name="id2526079"></a><a name="id2526085"></a><p> To create or access objects in a schema, write a
78
<i class="firstterm">qualified name</i> consisting of the schema name and
79
table name separated by a dot:
81
<pre class="synopsis"><i class="replaceable"><tt>schema</tt></i><tt class="literal">.</tt><i class="replaceable"><tt>table</tt></i></pre>
83
(For brevity we will speak of tables only, but the same ideas apply
84
to other kinds of named objects, such as types and functions.)
86
<p> Actually, the even more general syntax
88
<pre class="synopsis"><i class="replaceable"><tt>database</tt></i><tt class="literal">.</tt><i class="replaceable"><tt>schema</tt></i><tt class="literal">.</tt><i class="replaceable"><tt>table</tt></i></pre>
90
can be used too, but at present this is just for pro-forma compliance
91
with the SQL standard. If you write a database name, it must be the
92
same as the database you are connected to.
94
<p> So to create a table in the new schema, use
96
<pre class="programlisting">CREATE TABLE myschema.mytable (
100
This works anywhere a table name is expected, including the table
101
modification commands and the data access commands discussed in
102
the following chapters.
104
<a name="id2526156"></a><p> To drop a schema if it's empty (all objects in it have been
107
<pre class="programlisting">DROP SCHEMA myschema;</pre>
109
To drop a schema including all contained objects, use
111
<pre class="programlisting">DROP SCHEMA myschema CASCADE;</pre>
113
See <a href="ddl-depend.html" title="5.10.�Dependency Tracking">Section�5.10, “Dependency Tracking”</a> for a description of the general
114
mechanism behind this.
116
<p> Often you will want to create a schema owned by someone else
117
(since this is one of the ways to restrict the activities of your
118
users to well-defined namespaces). The syntax for that is:
120
<pre class="programlisting">CREATE SCHEMA <i class="replaceable"><tt>schemaname</tt></i> AUTHORIZATION <i class="replaceable"><tt>username</tt></i>;</pre>
122
You can even omit the schema name, in which case the schema name
123
will be the same as the user name. See <a href="ddl-schemas.html#ddl-schemas-patterns" title="5.8.6.�Usage Patterns">Section�5.8.6, “Usage Patterns”</a> for how this can be useful.
125
<p> Schema names beginning with <tt class="literal">pg_</tt> are reserved for
126
system purposes and may not be created by users.
129
<div class="sect2" lang="en">
130
<div class="titlepage">
131
<div><div><h3 class="title">
132
<a name="ddl-schemas-public"></a>5.8.2.�The Public Schema</h3></div></div>
135
<a name="id2526235"></a><p> In the previous sections we created tables without specifying any
136
schema names. By default, such tables (and other objects) are
137
automatically put into a schema named “<span class="quote">public</span>”. Every new
138
database contains such a schema. Thus, the following are equivalent:
140
<pre class="programlisting">CREATE TABLE products ( ... );</pre>
144
<pre class="programlisting">CREATE TABLE public.products ( ... );</pre>
148
<div class="sect2" lang="en">
149
<div class="titlepage">
150
<div><div><h3 class="title">
151
<a name="ddl-schemas-path"></a>5.8.3.�The Schema Search Path</h3></div></div>
154
<a name="id2526279"></a><a name="id2526284"></a><a name="id2526290"></a><p> Qualified names are tedious to write, and it's often best not to
155
wire a particular schema name into applications anyway. Therefore
156
tables are often referred to by <i class="firstterm">unqualified names</i>,
157
which consist of just the table name. The system determines which table
158
is meant by following a <i class="firstterm">search path</i>, which is a list
159
of schemas to look in. The first matching table in the search path
160
is taken to be the one wanted. If there is no match in the search
161
path, an error is reported, even if matching table names exist
162
in other schemas in the database.
164
<a name="id2526317"></a><p> The first schema named in the search path is called the current schema.
165
Aside from being the first schema searched, it is also the schema in
166
which new tables will be created if the <tt class="command">CREATE TABLE</tt>
167
command does not specify a schema name.
169
<a name="id2526337"></a><p> To show the current search path, use the following command:
171
<pre class="programlisting">SHOW search_path;</pre>
173
In the default setup this returns:
175
<pre class="screen"> search_path
179
The first element specifies that a schema with the same name as
180
the current user is to be searched. If no such schema exists,
181
the entry is ignored. The second element refers to the
182
public schema that we have seen already.
184
<p> The first schema in the search path that exists is the default
185
location for creating new objects. That is the reason that by
186
default objects are created in the public schema. When objects
187
are referenced in any other context without schema qualification
188
(table modification, data modification, or query commands) the
189
search path is traversed until a matching object is found.
190
Therefore, in the default configuration, any unqualified access
191
again can only refer to the public schema.
193
<p> To put our new schema in the path, we use
195
<pre class="programlisting">SET search_path TO myschema,public;</pre>
197
(We omit the <tt class="literal">$user</tt> here because we have no
198
immediate need for it.) And then we can access the table without
199
schema qualification:
201
<pre class="programlisting">DROP TABLE mytable;</pre>
203
Also, since <tt class="literal">myschema</tt> is the first element in
204
the path, new objects would by default be created in it.
206
<p> We could also have written
208
<pre class="programlisting">SET search_path TO myschema;</pre>
210
Then we no longer have access to the public schema without
211
explicit qualification. There is nothing special about the public
212
schema except that it exists by default. It can be dropped, too.
214
<p> See also <a href="functions-info.html" title="9.19.�System Information Functions">Section�9.19, “System Information Functions”</a> for other ways to access
215
the schema search path.
217
<p> The search path works in the same way for data type names, function names,
218
and operator names as it does for table names. Data type and function
219
names can be qualified in exactly the same way as table names. If you
220
need to write a qualified operator name in an expression, there is a
221
special provision: you must write
223
<pre class="synopsis"><tt class="literal">OPERATOR(</tt><i class="replaceable"><tt>schema</tt></i><tt class="literal">.</tt><i class="replaceable"><tt>operator</tt></i><tt class="literal">)</tt></pre>
225
This is needed to avoid syntactic ambiguity. An example is
227
<pre class="programlisting">SELECT 3 OPERATOR(pg_catalog.+) 4;</pre>
229
In practice one usually relies on the search path for operators,
230
so as not to have to write anything so ugly as that.
233
<div class="sect2" lang="en">
234
<div class="titlepage">
235
<div><div><h3 class="title">
236
<a name="ddl-schemas-priv"></a>5.8.4.�Schemas and Privileges</h3></div></div>
239
<a name="id2526477"></a><p> By default, users cannot access any objects in schemas they do not
240
own. To allow that, the owner of the schema needs to grant the
241
<tt class="literal">USAGE</tt> privilege on the schema. To allow users
242
to make use of the objects in the schema, additional privileges
243
may need to be granted, as appropriate for the object.
245
<p> A user can also be allowed to create objects in someone else's
246
schema. To allow that, the <tt class="literal">CREATE</tt> privilege on
247
the schema needs to be granted. Note that by default, everyone
248
has <tt class="literal">CREATE</tt> and <tt class="literal">USAGE</tt> privileges on
250
<tt class="literal">public</tt>. This allows all users that are able to
251
connect to a given database to create objects in its
252
<tt class="literal">public</tt> schema. If you do
253
not want to allow that, you can revoke that privilege:
255
<pre class="programlisting">REVOKE CREATE ON SCHEMA public FROM PUBLIC;</pre>
257
(The first “<span class="quote">public</span>” is the schema, the second
258
“<span class="quote">public</span>” means “<span class="quote">every user</span>”. In the
259
first sense it is an identifier, in the second sense it is a
260
reserved word, hence the different capitalization; recall the
261
guidelines from <a href="sql-syntax.html#sql-syntax-identifiers" title="4.1.1.�Identifiers and Key Words">Section�4.1.1, “Identifiers and Key Words”</a>.)
264
<div class="sect2" lang="en">
265
<div class="titlepage">
266
<div><div><h3 class="title">
267
<a name="ddl-schemas-catalog"></a>5.8.5.�The System Catalog Schema</h3></div></div>
270
<a name="id2526578"></a><p> In addition to <tt class="literal">public</tt> and user-created schemas, each
271
database contains a <tt class="literal">pg_catalog</tt> schema, which contains
272
the system tables and all the built-in data types, functions, and
273
operators. <tt class="literal">pg_catalog</tt> is always effectively part of
274
the search path. If it is not named explicitly in the path then
275
it is implicitly searched <span class="emphasis"><em>before</em></span> searching the path's
276
schemas. This ensures that built-in names will always be
277
findable. However, you may explicitly place
278
<tt class="literal">pg_catalog</tt> at the end of your search path if you
279
prefer to have user-defined names override built-in names.
281
<p> In <span class="productname">PostgreSQL</span> versions before 7.3,
282
table names beginning with <tt class="literal">pg_</tt> were reserved. This is
283
no longer true: you may create such a table name if you wish, in
284
any non-system schema. However, it's best to continue to avoid
285
such names, to ensure that you won't suffer a conflict if some
286
future version defines a system table named the same as your
287
table. (With the default search path, an unqualified reference to
288
your table name would be resolved as the system table instead.)
289
System tables will continue to follow the convention of having
290
names beginning with <tt class="literal">pg_</tt>, so that they will not
291
conflict with unqualified user-table names so long as users avoid
292
the <tt class="literal">pg_</tt> prefix.
295
<div class="sect2" lang="en">
296
<div class="titlepage">
297
<div><div><h3 class="title">
298
<a name="ddl-schemas-patterns"></a>5.8.6.�Usage Patterns</h3></div></div>
301
<p> Schemas can be used to organize your data in many ways. There are
302
a few usage patterns that are recommended and are easily supported by
303
the default configuration:
305
<div class="itemizedlist"><ul type="disc">
306
<li><p> If you do not create any schemas then all users access the
307
public schema implicitly. This simulates the situation where
308
schemas are not available at all. This setup is mainly
309
recommended when there is only a single user or a few cooperating
310
users in a database. This setup also allows smooth transition
311
from the non-schema-aware world.
314
<p> You can create a schema for each user with the same name as
315
that user. Recall that the default search path starts with
316
<tt class="literal">$user</tt>, which resolves to the user name.
317
Therefore, if each user has a separate schema, they access their
318
own schemas by default.
320
<p> If you use this setup then you might also want to revoke access
321
to the public schema (or drop it altogether), so users are
322
truly constrained to their own schemas.
325
<li><p> To install shared applications (tables to be used by everyone,
326
additional functions provided by third parties, etc.), put them
327
into separate schemas. Remember to grant appropriate
328
privileges to allow the other users to access them. Users can
329
then refer to these additional objects by qualifying the names
330
with a schema name, or they can put the additional schemas into
331
their search path, as they choose.
337
<div class="sect2" lang="en">
338
<div class="titlepage">
339
<div><div><h3 class="title">
340
<a name="ddl-schemas-portability"></a>5.8.7.�Portability</h3></div></div>
343
<p> In the SQL standard, the notion of objects in the same schema
344
being owned by different users does not exist. Moreover, some
345
implementations do not allow you to create schemas that have a
346
different name than their owner. In fact, the concepts of schema
347
and user are nearly equivalent in a database system that
348
implements only the basic schema support specified in the
349
standard. Therefore, many users consider qualified names to
351
<tt class="literal"><i class="replaceable"><tt>username</tt></i>.<i class="replaceable"><tt>tablename</tt></i></tt>.
352
This is how <span class="productname">PostgreSQL</span> will effectively
353
behave if you create a per-user schema for every user.
355
<p> Also, there is no concept of a <tt class="literal">public</tt> schema in the
356
SQL standard. For maximum conformance to the standard, you should
357
not use (perhaps even remove) the <tt class="literal">public</tt> schema.
359
<p> Of course, some SQL database systems might not implement schemas
360
at all, or provide namespace support by allowing (possibly
361
limited) cross-database access. If you need to work with those
362
systems, then maximum portability would be achieved by not using