Skip to content

PostgreSQL SCHEMA

    CREATE SCHEMA

    CREATE SCHEMA — define a new schema

    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_USER

      | SESSION_USER

    CREATE SCHEMA enters a new schema into the current database. The schema name must be distinct from the name of any existing schema in the current database.

    A schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can duplicate those of other objects existing in other schemas. 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). 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 can be determined with the function current_schema).

    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.

    Parameters

    schema_name

    The name of a schema to be created. If this is omitted, the user_name is used as the schema name. The name cannot begin with pg_, as such names are reserved for system schemas.

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

    IF NOT EXISTS

    Do nothing (except issuing a notice) if a schema with the same name already exists. schema_element subcommands cannot be included when this option is used.

    To create a schema, the invoking user must have the CREATE privilege for the current database. (Of course, superusers bypass this check.)

    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;

    ALTER SCHEMA

    ALTER SCHEMA — change the definition of a schema

    ALTER SCHEMA name RENAME TO new_name

    ALTER SCHEMA name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }

    ALTER SCHEMA changes the definition of a schema.

    You must own the schema to use ALTER SCHEMA. To rename a schema you must also have the CREATE privilege for the database. To alter the owner, you must also be a direct or indirect member of the new owning role, and you must have the CREATE privilege for the database. (Note that superusers have all these privileges automatically.)

    Parameters

    name

    The name of an existing schema.

    new_name

    The new name of the schema. The new name cannot begin with pg_, as such names are reserved for system schemas.

    new_owner

    The new owner of the schema.

    DROP SCHEMA

    DROP SCHEMA — remove a schema

    DROP SCHEMA [ IF EXISTS ] name [, …] [ CASCADE | RESTRICT ]

    DROP SCHEMA removes schemas from the database.

    A schema can only be dropped by its owner or a superuser. Note that the owner can drop the schema (and thereby all contained objects) even if they do not own some of the objects within the schema.

    Parameters

    IF EXISTS

    Do not throw an error if the schema does not exist. A notice is issued in this case.

    name

    The name of a schema.

    CASCADE

    Automatically drop objects (tables, functions, etc.) that are contained in the schema, and in turn all objects that depend on those objects.

    RESTRICT

    Refuse to drop the schema if it contains any objects. This is the default.

    Using the CASCADE option might make the command remove objects in other schemas besides the one(s) named.

    Examples

    To remove schema mystuff from the database, along with everything it contains:

    DROP SCHEMA mystuff CASCADE;