Difference between revisions of "PostgreSQL: CREATE SCHEMA"

From wikieduonline
Jump to navigation Jump to search
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
* https://www.postgresql.org/docs/current/sql-createschema.html
 
* https://www.postgresql.org/docs/current/sql-createschema.html
<pre>
 
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:
+
[[CREATE]] [[SCHEMA]] schema_name [ AUTHORIZATION [[role_specification]] ] [ schema_element [ ... ] ]
 
+
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
    user_name
+
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
  | CURRENT_ROLE
+
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
  | CURRENT_USER
+
  | SESSION_USER
+
where role_specification can be:
</pre>
+
 +
    user_name
 +
  | CURRENT_ROLE
 +
  | CURRENT_USER
 +
  | SESSION_USER
  
 
== Examples ==
 
== Examples ==
Line 19: Line 18:
  
 
  CREATE SCHEMA myschema;
 
  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 SCHEMA IF NOT EXISTS [[datadog]];
  
 
  CREATE SCHEMA AUTHORIZATION 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 SCHEMA prometheus AUTHORIZATION prometheus;
Create a schema and create a table and view within it:
 
  
 
  CREATE SCHEMA hollywood
 
  CREATE SCHEMA hollywood
Line 33: Line 32:
 
Notice that the individual subcommands do not end with semicolons.
 
Notice that the individual subcommands do not end with semicolons.
  
 +
== Related ==
 +
* [[List]] schemas: <code>[[\dn]]</code>
 +
* [[GRANT USAGE ON SCHEMA]]
 +
* [[Amazon Redshift: CREATE SCHEMA]]
  
 
== See also ==
 
== See also ==

Latest revision as of 11:27, 8 February 2023

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[edit]

Examples Create a schema:

CREATE SCHEMA myschema;
CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
CREATE SCHEMA IF NOT EXISTS datadog;
CREATE SCHEMA AUTHORIZATION joe;
CREATE SCHEMA prometheus AUTHORIZATION prometheus;
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[edit]

See also[edit]

Advertising: