1CREATE SCHEMA(7) PostgreSQL 11.6 Documentation CREATE SCHEMA(7)
2
3
4
6 CREATE_SCHEMA - define a new schema
7
9 CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
10 CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
11 CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
12 CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
13
14 where role_specification can be:
15
16 user_name
17 | CURRENT_USER
18 | SESSION_USER
19
21 CREATE SCHEMA enters a new schema into the current database. The schema
22 name must be distinct from the name of any existing schema in the
23 current database.
24
25 A schema is essentially a namespace: it contains named objects (tables,
26 data types, functions, and operators) whose names can duplicate those
27 of other objects existing in other schemas. Named objects are accessed
28 either by “qualifying” their names with the schema name as a prefix, or
29 by setting a search path that includes the desired schema(s). A CREATE
30 command specifying an unqualified object name creates the object in the
31 current schema (the one at the front of the search path, which can be
32 determined with the function current_schema).
33
34 Optionally, CREATE SCHEMA can include subcommands to create objects
35 within the new schema. The subcommands are treated essentially the same
36 as separate commands issued after creating the schema, except that if
37 the AUTHORIZATION clause is used, all the created objects will be owned
38 by that user.
39
41 schema_name
42 The name of a schema to be created. If this is omitted, the
43 user_name is used as the schema name. The name cannot begin with
44 pg_, as such names are reserved for system schemas.
45
46 user_name
47 The role name of the user who will own the new schema. If omitted,
48 defaults to the user executing the command. To create a schema
49 owned by another role, you must be a direct or indirect member of
50 that role, or be a superuser.
51
52 schema_element
53 An SQL statement defining an object to be created within the
54 schema. Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX,
55 CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as clauses
56 within CREATE SCHEMA. Other kinds of objects may be created in
57 separate commands after the schema is created.
58
59 IF NOT EXISTS
60 Do nothing (except issuing a notice) if a schema with the same name
61 already exists. schema_element subcommands cannot be included when
62 this option is used.
63
65 To create a schema, the invoking user must have the CREATE privilege
66 for the current database. (Of course, superusers bypass this check.)
67
69 Create a schema:
70
71 CREATE SCHEMA myschema;
72
73 Create a schema for user joe; the schema will also be named joe:
74
75 CREATE SCHEMA AUTHORIZATION joe;
76
77 Create a schema named test that will be owned by user joe, unless there
78 already is a schema named test. (It does not matter whether joe owns
79 the pre-existing schema.)
80
81 CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
82
83 Create a schema and create a table and view within it:
84
85 CREATE SCHEMA hollywood
86 CREATE TABLE films (title text, release date, awards text[])
87 CREATE VIEW winners AS
88 SELECT title, release FROM films WHERE awards IS NOT NULL;
89
90 Notice that the individual subcommands do not end with semicolons.
91
92 The following is an equivalent way of accomplishing the same result:
93
94 CREATE SCHEMA hollywood;
95 CREATE TABLE hollywood.films (title text, release date, awards text[]);
96 CREATE VIEW hollywood.winners AS
97 SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
98
100 The SQL standard allows a DEFAULT CHARACTER SET clause in CREATE
101 SCHEMA, as well as more subcommand types than are presently accepted by
102 PostgreSQL.
103
104 The SQL standard specifies that the subcommands in CREATE SCHEMA can
105 appear in any order. The present PostgreSQL implementation does not
106 handle all cases of forward references in subcommands; it might
107 sometimes be necessary to reorder the subcommands in order to avoid
108 forward references.
109
110 According to the SQL standard, the owner of a schema always owns all
111 objects within it. PostgreSQL allows schemas to contain objects owned
112 by users other than the schema owner. This can happen only if the
113 schema owner grants the CREATE privilege on their schema to someone
114 else, or a superuser chooses to create objects in it.
115
116 The IF NOT EXISTS option is a PostgreSQL extension.
117
119 ALTER SCHEMA (ALTER_SCHEMA(7)), DROP SCHEMA (DROP_SCHEMA(7))
120
121
122
123PostgreSQL 11.6 2019 CREATE SCHEMA(7)