]> begriffs open source - ai-pg/blob - full-docs/txt/sql-createschema.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-createschema.txt
1
2 CREATE SCHEMA
3
4    CREATE SCHEMA — define a new schema
5
6 Synopsis
7
8 CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element
9 [ ... ] ]
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
21 Description
22
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
25    current database.
26
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).
35
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
40    by that user.
41
42 Parameters
43
44    schema_name
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.
48
49    user_name
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
53           that role.
54
55    schema_element
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.
61
62    IF NOT EXISTS
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.
66
67 Notes
68
69    To create a schema, the invoking user must have the CREATE privilege
70    for the current database. (Of course, superusers bypass this check.)
71
72 Examples
73
74    Create a schema:
75 CREATE SCHEMA myschema;
76
77    Create a schema for user joe; the schema will also be named joe:
78 CREATE SCHEMA AUTHORIZATION joe;
79
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;
84
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;
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 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
99 Compatibility
100
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
119 See Also
120
121    ALTER SCHEMA, DROP SCHEMA