CREATE_SCHEMA(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

πŸ’– 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, the user_name is used as the schema name. and ngl, the name cannot begin with pg_, 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, only CREATE TABLE, CREATE VIEW, CREATE INDEX, CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as clauses within CREATE 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_element subcommands 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 🌈


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