1CREATE SCHEMA(7) SQL Commands CREATE SCHEMA(7)
2
3
4
6 CREATE SCHEMA - define a new schema
7
8
10 CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ]
11 CREATE SCHEMA AUTHORIZATION username [ schema_element [ ... ] ]
12
13
15 CREATE SCHEMA enters a new schema into the current database. The
16 schema name must be distinct from the name of any existing schema in
17 the current database.
18
19 A schema is essentially a namespace: it contains named objects (tables,
20 data types, functions, and operators) whose names can duplicate those
21 of other objects existing in other schemas. Named objects are accessed
22 either by ``qualifying'' their names with the schema name as a prefix,
23 or by setting a search path that includes the desired schema(s). A CRE‐
24 ATE command specifying an unqualified object name creates the object in
25 the current schema (the one at the front of the search path, which can
26 be determined with the function current_schema).
27
28 Optionally, CREATE SCHEMA can include subcommands to create objects
29 within the new schema. The subcommands are treated essentially the same
30 as separate commands issued after creating the schema, except that if
31 the AUTHORIZATION clause is used, all the created objects will be owned
32 by that user.
33
35 schemaname
36 The name of a schema to be created. If this is omitted, the user
37 name is used as the schema name. The name cannot begin with pg_,
38 as such names are reserved for system schemas.
39
40 username
41 The name of the user who will own the schema. If omitted,
42 defaults to the user executing the command. Only superusers can
43 create schemas owned by users other than themselves.
44
45 schema_element
46 An SQL statement defining an object to be created within the
47 schema. Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX,
48 CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as
49 clauses within CREATE SCHEMA. Other kinds of objects may be cre‐
50 ated in separate commands after the schema is created.
51
53 To create a schema, the invoking user must have the CREATE privilege
54 for the current database. (Of course, superusers bypass this check.)
55
57 Create a schema:
58
59 CREATE SCHEMA myschema;
60
61
62 Create a schema for user joe; the schema will also be named joe:
63
64 CREATE SCHEMA AUTHORIZATION joe;
65
66
67 Create a schema and create a table and view within it:
68
69 CREATE SCHEMA hollywood
70 CREATE TABLE films (title text, release date, awards text[])
71 CREATE VIEW winners AS
72 SELECT title, release FROM films WHERE awards IS NOT NULL;
73
74 Notice that the individual subcommands do not end with semicolons.
75
76 The following is an equivalent way of accomplishing the same result:
77
78 CREATE SCHEMA hollywood;
79 CREATE TABLE hollywood.films (title text, release date, awards text[]);
80 CREATE VIEW hollywood.winners AS
81 SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
82
83
85 The SQL standard allows a DEFAULT CHARACTER SET clause in CREATE
86 SCHEMA, as well as more subcommand types than are presently accepted by
87 PostgreSQL.
88
89 The SQL standard specifies that the subcommands in CREATE SCHEMA can
90 appear in any order. The present PostgreSQL implementation does not
91 handle all cases of forward references in subcommands; it might some‐
92 times be necessary to reorder the subcommands in order to avoid forward
93 references.
94
95 According to the SQL standard, the owner of a schema always owns all
96 objects within it. PostgreSQL allows schemas to contain objects owned
97 by users other than the schema owner. This can happen only if the
98 schema owner grants the CREATE privilege on his schema to someone else.
99
101 ALTER SCHEMA [alter_schema(7)], DROP SCHEMA [drop_schema(7)]
102
103
104
105SQL - Language Statements 2011-09-22 CREATE SCHEMA(7)