CREATE_SCHEMA(7pg17)

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

πŸ’– CREATE_SCHEMA πŸ’–

ok so bestie, welcome to the CREATE_SCHEMA(7) glow-up β€” this is the PostgreSQL 17.11 Documentation page for creating a new schema, but make it make sense. literally the whole manual is here, nothing skipped, i promise on my life 🫢


✨ NAME ✨

CREATE_SCHEMA β€” define a new schema. yes bestie, you're literally about to invent a whole new namespace. main character energy fr fr πŸ‘‘


🌸 SYNOPSIS 🌸

ok here's the vibe check, copy these exactly:

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 role_specification can be:

    user_name
  | CURRENT_ROLE
  | CURRENT_USER
  | SESSION_USER

that's the whole menu, no cap πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

CREATE SCHEMA enters a new schema into the current database. periodt. and the schema name has to be distinct from the name of any existing schema in the current database β€” so no stealing names, that's not slay behavior πŸ™ˆ

a schema is essentially a namespace, okay? think of it like a little folder that contains named objects β€” tables, data types, functions, and operators β€” and those names can duplicate the names of other objects that already exist in other schemas. iconic, truly. you access named objects either by β€œqualifying” their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s). a CREATE command that specifies an unqualified object name creates the object in the current schema β€” that's the one at the front of the search path, which you can figure out with the function current_schema.

optionally, CREATE SCHEMA can include subcommands to create objects within the new schema. those 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 ownership bundle πŸŽ€


🧚 PARAMETERS 🧚

  • schema_name β€” the name of a schema to be created. if this is omitted, user_name is used as the schema name. and listen β€” the name cannot begin with pg_, because those names are reserved for system schemas. do not fight the system, she will win πŸ’€
  • user_name β€” the role name of the user who will own the new schema. if omitted, defaults to the user executing the command. to create a schema owned by another role, you must be able to SET ROLE to that role. iykyk πŸ”₯
  • 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 β€” so like, do the schema first, then go feral with the other objects πŸ¦‹
  • IF NOT EXISTS β€” do nothing (except issuing a notice) if a schema with the same name already exists. and heads up: schema_element subcommands cannot be included when this option is used. no mixing, bestie πŸ™…

πŸ’œ NOTES πŸ’œ

to create a schema, the invoking user must have the CREATE privilege for the current database. (of course, superusers bypass this check β€” the audacity, the power, we love to see it πŸ‘‘)


🌈 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.)

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. not me almost adding them πŸ™ˆ

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;

same result, two paths, both valid, both ate πŸ”₯


⭐ 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. so yeah, standard girlies get extra stuff, we don't, and that's just how it is 🫢

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. lowkey annoying but now you know πŸ’€

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. generous behavior, honestly πŸŽ€

the IF NOT EXISTS option is a PostgreSQL extension.


🌸 SEE ALSO 🌸

go read those next, bestie, you're on a roll ✨


ok that's literally the entire page, every flag, every caveat, every example β€” nothing dropped, nothing invented. slay πŸ’…

πŸ’– that's the whole manual, bestie β€” PostgreSQL 17.11, dated 2026, CREATE SCHEMA(7). now go create some schemas and be iconic πŸ’–

πŸ“¦ where this page comes from