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