CREATE_SCHEMA(7pg17)
π 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_nameis used as the schema name. and listen β the name cannot begin withpg_, 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 toSET ROLEto that role. iykyk π₯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 β 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_elementsubcommands 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 πΈ
ALTER SCHEMA(ALTER_SCHEMA(7))DROP SCHEMA(DROP_SCHEMA(7))
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
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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