postgresql_16:7.1.78.4 Examples
PostgreSQL 16 > Reference > SQL Commands > CREATE SCHEMA > 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;