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
37 username is used as the schema name. The name cannot begin with
38 pg_, as such names are reserved for system schemas.
39
40 username
41 The role name of the user who will own the new schema. If omit‐
42 ted, defaults to the user executing the command. To create a
43 schema owned by another role, you must be a direct or indirect
44 member of that role, or be a superuser.
45
46 schema_element
47 An SQL statement defining an object to be created within the
48 schema. Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX,
49 CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as
50 clauses within CREATE SCHEMA. Other kinds of objects may be cre‐
51 ated in separate commands after the schema is created.
52
54 To create a schema, the invoking user must have the CREATE privilege
55 for the current database. (Of course, superusers bypass this check.)
56
58 Create a schema:
59
60 CREATE SCHEMA myschema;
61
62
63 Create a schema for user joe; the schema will also be named joe:
64
65 CREATE SCHEMA AUTHORIZATION joe;
66
67
68 Create a schema and create a table and view within it:
69
70 CREATE SCHEMA hollywood
71 CREATE TABLE films (title text, release date, awards text[])
72 CREATE VIEW winners AS
73 SELECT title, release FROM films WHERE awards IS NOT NULL;
74
75 Notice that the individual subcommands do not end with semicolons.
76
77 The following is an equivalent way of accomplishing the same result:
78
79 CREATE SCHEMA hollywood;
80 CREATE TABLE hollywood.films (title text, release date, awards text[]);
81 CREATE VIEW hollywood.winners AS
82 SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
83
84
86 The SQL standard allows a DEFAULT CHARACTER SET clause in CREATE
87 SCHEMA, as well as more subcommand types than are presently accepted by
88 PostgreSQL.
89
90 The SQL standard specifies that the subcommands in CREATE SCHEMA can
91 appear in any order. The present PostgreSQL implementation does not
92 handle all cases of forward references in subcommands; it might some‐
93 times be necessary to reorder the subcommands in order to avoid forward
94 references.
95
96 According to the SQL standard, the owner of a schema always owns all
97 objects within it. PostgreSQL allows schemas to contain objects owned
98 by users other than the schema owner. This can happen only if the
99 schema owner grants the CREATE privilege on his schema to someone else,
100 or a superuser chooses to create objects in it.
101
103 ALTER SCHEMA [alter_schema(7)], DROP SCHEMA [drop_schema(7)]
104
105
106
107SQL - Language Statements 2014-02-17 CREATE SCHEMA(7)