CREATE_SCHEMA(7pg16)

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

πŸ’– CREATE_SCHEMA β€” define a new schema πŸ’–

okay so this is the PostgreSQL 16.15 documentation page for CREATE_SCHEMA, and bestie, it's literally your cheat code for spinning up brand-new schemas. let's get into it 🫢


✨ NAME ✨

CREATE_SCHEMA β€” define a new schema πŸ’…


✨ 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

where role_specification can be:

    user_name
  | CURRENT_ROLE
  | CURRENT_USER
  | SESSION_USER

that last bit is important β€” role_specification can be any one of those four things, ok? don't sleep on it πŸŽ€


✨ DESCRIPTION ✨

So CREATE SCHEMA enters a new schema into the current database. the schema name has to be distinct from the name of any existing schema in the current database β€” no duplicates allowed, periodt 🚫

A schema is basically a namespace: it holds named objects (tables, data types, functions, and operators) whose names are allowed to duplicate names of other objects chillin' in other schemas. You get to those named objects either by "qualifying" their names with the schema name as a prefix, or by setting a search path that includes the schema(s) you want. And a CREATE command with 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 (iconic behaviour honestly), CREATE SCHEMA can include subcommands to create objects inside 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. slay πŸ‘‘


✨ PARAMETERS ✨

  • schema_name β€” the name of the schema you're creating. if you omit it, user_name is used as the schema name. lowkey important: the name cannot begin with pg_, because those names are reserved for system schemas 🚫
  • user_name β€” the role name of the user who will own the new schema. if omitted, it defaults to the user executing the command. and if you wanna create a schema owned by another role, you gotta be able to SET ROLE to that role, iykyk
  • schema_element β€” an SQL statement defining an object to be created within the schema. rn, only CREATE TABLE, CREATE VIEW, CREATE INDEX, CREATE SEQUENCE, CREATE TRIGGER and GRANT are accepted as clauses within CREATE SCHEMA. other kinds of objects can be created in separate commands after the schema is created 🌸
  • IF NOT EXISTS β€” do nothing (except issuing a notice) if a schema with the same name already exists. ✨ but heads up: schema_element subcommands cannot be included when this option is used πŸ’€

✨ NOTES ✨

To create a schema, the invoking user must have the CREATE privilege for the current database. (and ofc, superusers bypass this check β€” main character energy fr πŸ’…)


✨ 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. πŸŽ€

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. πŸ™ˆ

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, keep that in mind bestie 😭

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. πŸ‘‘

The IF NOT EXISTS option is a PostgreSQL extension. ⭐


✨ SEE ALSO ✨

ALTER SCHEMA (ALTER_SCHEMA(7)), DROP SCHEMA (DROP_SCHEMA(7)) πŸ¦‹


and that's the whole page, bestie!! this one's from PostgreSQL 16.15, dated 2026 β€” go forth and make schemas, you absolute icon πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from