CREATE_SCHEMA(7pg14)
π CREATE_SCHEMA π
okay bestie, welcome to the CREATE SCHEMA page β that's the PostgreSQL 14.24 Documentation version, section 7, and yes she's iconic. quick vibes summary: this is the command that lets you define a new schema. that's it. that's the whole personality. π
β¨ SYNOPSIS β¨
CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
and like, role_specification can be any of these girlies:
user_name
| CURRENT_ROLE
| CURRENT_USER
| SESSION_USER
π¦ DESCRIPTION π¦
so CREATE SCHEMA literally just enters a new schema into the current database. that's the whole thing. the schema name has to be distinct from the name of any existing schema in the current database β no duplicates allowed, the vibe check is strict π
a schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can totally duplicate those of other objects living in other schemas. no drama, they're in different houses. named objects are accessed either by βqualifyingβ their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s). and a CREATE command specifying an unqualified object name creates the object in the current schema (the one at the front of the search path, which you can figure out with the function current_schema). iykyk β¨
optionally, CREATE SCHEMA can include subcommands to create objects within the new schema. the subcommands are treated essentially the same as separate commands issued after creating the schema β except that if the AUTHORIZATION clause is used, all the created objects will be owned by that user. cute little plot twist π«Ά
πΈ PARAMETERS πΈ
schema_nameβ the name of a schema to be created. if this is omitted, theuser_nameis used as the schema name. and ngl, the name cannot begin withpg_, because those names are reserved for system schemas. respect the system, bestie πuser_nameβ the role name of the user who will own the new schema. if omitted, it defaults to the user executing the command. to create a schema owned by another role, you must be a direct or indirect member of that role, or be a superuser. no sneaking in, fr fr π₯schema_elementβ an SQL statement defining an object to be created within the schema. currently, onlyCREATE TABLE,CREATE VIEW,CREATE INDEX,CREATE SEQUENCE,CREATE TRIGGERandGRANTare accepted as clauses withinCREATE SCHEMA. other kinds of objects may be created in separate commands after the schema is created β take it up with them later, bestie πIF NOT EXISTSβ do nothing (except issuing a notice) if a schema with the same name already exists. > β οΈ careful:schema_elementsubcommands cannot be included when this option is used. like, not at all. don't try it π
β NOTES β
to create a schema, the invoking user must have the CREATE privilege for the current database. (and of course, superusers bypass this check β main character energy, they don't wait in line π)
π EXAMPLES π
create a schema:
CREATE SCHEMA myschema;
create a schema for user joe; the schema will also be named joe:
CREATE SCHEMA AUTHORIZATION joe;
create a schema named test that will be owned by user joe, unless there already is a schema named test. (it does not matter whether joe owns the pre-existing schema β we simply do not care π
)
CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
create a schema and create a table and view within it:
CREATE SCHEMA hollywood
CREATE TABLE films (title text, release date, awards text[])
CREATE VIEW winners AS
SELECT title, release FROM films WHERE awards IS NOT NULL;
notice that the individual subcommands do not end with semicolons. literally none of them. no semicolons in sight π§
the following is an equivalent way of accomplishing the same result:
CREATE SCHEMA hollywood;
CREATE TABLE hollywood.films (title text, release date, awards text[]);
CREATE VIEW hollywood.winners AS
SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;
π COMPATIBILITY π
the SQL standard allows a DEFAULT CHARACTER SET clause in CREATE SCHEMA, as well as more subcommand types than are presently accepted by PostgreSQL. we're built different, standard, sorry not sorry.
the SQL standard specifies that the subcommands in CREATE SCHEMA can appear in any order. the present PostgreSQL implementation does not handle all cases of forward references in subcommands; it might sometimes be necessary to reorder the subcommands in order to avoid forward references. so like, sometimes you gotta shuffle the deck. annnoying? yes. fixable? also yes β¨
according to the SQL standard, the owner of a schema always owns all objects within it. PostgreSQL allows schemas to contain objects owned by users other than the schema owner. this can happen only if the schema owner grants the CREATE privilege on their schema to someone else, or a superuser chooses to create objects in it. sharing is caring, ig π«Ά
π‘ the IF NOT EXISTS option is a PostgreSQL extension. not standard, just us. slay.
π SEE ALSO π
ALTER SCHEMA(ALTER_SCHEMA(7))DROP SCHEMA(DROP_SCHEMA(7))
and that's the page, bestie π go forth and namespace responsibly. this has been CREATE SCHEMA(7), straight out of the PostgreSQL 14.24 docs, dated 2026 β mwah, love u, bye πππ¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms