4 CREATE SCHEMA — define a new schema
8 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
14 where role_specification can be:
23 CREATE SCHEMA enters a new schema into the current database. The schema
24 name must be distinct from the name of any existing schema in the
27 A schema is essentially a namespace: it contains named objects (tables,
28 data types, functions, and operators) whose names can duplicate those
29 of other objects existing in other schemas. Named objects are accessed
30 either by “qualifying” their names with the schema name as a prefix, or
31 by setting a search path that includes the desired schema(s). A CREATE
32 command specifying an unqualified object name creates the object in the
33 current schema (the one at the front of the search path, which can be
34 determined with the function current_schema).
36 Optionally, CREATE SCHEMA can include subcommands to create objects
37 within the new schema. The subcommands are treated essentially the same
38 as separate commands issued after creating the schema, except that if
39 the AUTHORIZATION clause is used, all the created objects will be owned
45 The name of a schema to be created. If this is omitted, the
46 user_name is used as the schema name. The name cannot begin with
47 pg_, as such names are reserved for system schemas.
50 The role name of the user who will own the new schema. If
51 omitted, defaults to the user executing the command. To create a
52 schema owned by another role, you must be able to SET ROLE to
56 An SQL statement defining an object to be created within the
57 schema. Currently, only CREATE TABLE, CREATE VIEW, CREATE INDEX,
58 CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as
59 clauses within CREATE SCHEMA. Other kinds of objects may be
60 created in separate commands after the schema is created.
63 Do nothing (except issuing a notice) if a schema with the same
64 name already exists. schema_element subcommands cannot be
65 included when this option is used.
69 To create a schema, the invoking user must have the CREATE privilege
70 for the current database. (Of course, superusers bypass this check.)
75 CREATE SCHEMA myschema;
77 Create a schema for user joe; the schema will also be named joe:
78 CREATE SCHEMA AUTHORIZATION joe;
80 Create a schema named test that will be owned by user joe, unless there
81 already is a schema named test. (It does not matter whether joe owns
82 the pre-existing schema.)
83 CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
85 Create a schema and create a table and view within it:
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;
91 Notice that the individual subcommands do not end with semicolons.
93 The following is an equivalent way of accomplishing the same result:
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;
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
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
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.
117 The IF NOT EXISTS option is a PostgreSQL extension.
121 ALTER SCHEMA, DROP SCHEMA