CREATE_SCHEMA(7pg15)
β¨ CREATE SCHEMA π
okay so bestie, welcome to CREATE SCHEMA from the PostgreSQL 15.19 docs β this is the page that teaches you how to make a whole new namespace in your database, and honestly? it's giving main character energy. 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
like, yes, there are four whole shapes this command can take. pick your fighter π
π¦ DESCRIPTION π¦
so CREATE SCHEMA enters a brand-new schema into the current database. and the vibe check here is strict: the schema name has to be distinct from the name of any existing schema in the current database. no duplicates, periodt π
a schema is essentially a namespace β it contains named objects (tables, data types, functions, and operators) whose names are allowed to duplicate the names of other objects that live in other schemas. iconic, honestly. you access 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 that specifies 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 that create objects inside the new schema. those subcommands are treated essentially the same as separate commands issued after creating the schema β with one big exception: if the AUTHORIZATION clause is used, all the created objects will be owned by that user. so like, they all match, no cap β¨
π PARAMETERS π
schema_nameβ the name of a schema to be created. if this is omitted, theuser_nameis used as the schema name. π¨ and heads up: the name cannot begin withpg_, because those names are reserved for system schemas. don't try it bestie.user_nameβ the role name of the user who will own the new schema. if it's 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. πschema_elementβ an SQL statement defining an object to be created within the schema. right now, onlyCREATE TABLE,CREATE VIEW,CREATE INDEX,CREATE SEQUENCE,CREATE TRIGGERandGRANTare accepted as clauses withinCREATE SCHEMA. other kinds of objects can be created in separate commands after the schema is created. so yeah, that's the whole list, don't come for me πIF NOT EXISTSβ do nothing (except issuing a notice) if a schema with the same name already exists. π but lowkey important:schema_elementsubcommands 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. (of course, superusers bypass this check β they always do π)
π₯ 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. that's the whole thing, bestie.
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. so, we're doing our own thing a lil bit π§
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 things around. not a bug, just the vibe π
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. β¨ (ours, our invention, we're obsessed)
π SEE ALSO π
ALTER SCHEMA(ALTER_SCHEMA(7))DROP SCHEMA(DROP_SCHEMA(7))
and that's the page, bestie π stay schemin'. mwah π
PostgreSQL 15.19 Β· 2026 β that's the version and date right there, straight from the footer, love u bye π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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