Difference between revisions of "PostgreSQL: CREATE SCHEMA"

From wikieduonline
Jump to navigation Jump to search
Line 35: Line 35:
 
== Related ==
 
== Related ==
 
* [[List]] schemas: <code>[[\dn]]</code>
 
* [[List]] schemas: <code>[[\dn]]</code>
 +
* [[GRANT]]
  
 
== See also ==
 
== See also ==

Revision as of 09:18, 28 April 2022

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

Examples

Examples Create a schema:

CREATE SCHEMA myschema;
Create a schema for user joe; the schema will also be named joe:
CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
Create a schema and create a table and view within it
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 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.

Related

See also

Advertising: