PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - CREATE SCHEMA

In PostgreSQL, a schema is essentially a namespace that allows users to create and manage database objects (like tables, views, indexes, etc.) under a logical group. Schemas are useful for organizing database objects and can also be used to manage access permissions.

The CREATE SCHEMA command is used to create a new schema in a database.

Syntax:

CREATE SCHEMA [IF NOT EXISTS] schema_name [AUTHORIZATION owner_name] [schema_element [...]];
  • IF NOT EXISTS: Allows you to create the schema only if it doesn't already exist. This is useful to avoid errors in scripts that might try to create a schema multiple times.

  • schema_name: The name you want to give to the schema.

  • AUTHORIZATION owner_name: Specifies the owner of the schema. If omitted, the current user is the default owner.

  • schema_element: You can also include schema elements (like table, view definitions) as part of the schema creation. This is less commonly used in practice.

Examples:

  1. Basic schema creation:

    CREATE SCHEMA myschema;
    
  2. Create schema with specific owner:

    CREATE SCHEMA myschema AUTHORIZATION myuser;
    
  3. Create schema and define a table within it:

    CREATE SCHEMA myschema 
    CREATE TABLE mytable (id INT PRIMARY KEY, name TEXT);
    

Accessing Objects in Schemas:

Once you have objects inside a schema, you reference them using the schema's name as a prefix:

SELECT * FROM myschema.mytable;

Notes:

  • By default, when a user creates an object without specifying a schema, PostgreSQL will use the public schema.

  • You can manage access permissions at the schema level. This allows you to group objects by purpose or by user/team and manage permissions more easily.

  • Remember to set the search_path appropriately, especially if you're working with multiple schemas in a database. The search_path determines in which schemas PostgreSQL should look for objects when an unqualified object name is used. For example:

    SET search_path TO myschema,public;
    

    This tells PostgreSQL to look in myschema first and then in public when trying to resolve object names.

  • Always design the schema structure keeping in mind the logical organization of data, future growth, and the security/access needs of different users or teams.

  1. Creating a new schema in PostgreSQL:

    • Description: Creates a new schema to organize database objects.
    • Code:
      CREATE SCHEMA your_schema;
      
  2. Setting the owner of a schema in PostgreSQL:

    • Description: Specifies the owner of the schema.
    • Code:
      CREATE SCHEMA your_schema AUTHORIZATION your_user;
      
  3. Defining tables within a specific schema in PostgreSQL:

    • Description: Creates tables within a specific schema.
    • Code:
      CREATE TABLE your_schema.your_table (
          column1 datatype,
          column2 datatype
      );
      
  4. Creating schemas with different character sets and collations in PostgreSQL:

    • Description: Sets the character set and collation for the schema.
    • Code:
      CREATE SCHEMA your_schema
      AUTHORIZATION your_user
      ENCODING 'UTF8'
      LC_COLLATE='en_US.UTF-8'
      LC_CTYPE='en_US.UTF-8';
      
  5. Granting permissions on schemas in PostgreSQL:

    • Description: Assigns privileges to users or roles on the schema.
    • Code:
      GRANT USAGE, CREATE ON SCHEMA your_schema TO your_user;
      
  6. Viewing existing schemas in PostgreSQL:

    • Description: Lists all existing schemas in the database.
    • Code:
      \dn
      
  7. Altering schemas with ALTER SCHEMA in PostgreSQL:

    • Description: Modifies the attributes of an existing schema.
    • Code:
      ALTER SCHEMA your_schema RENAME TO new_schema;
      ALTER SCHEMA your_schema OWNER TO new_owner;
      
  8. Dropping schemas with DROP SCHEMA in PostgreSQL:

    • Description: Removes a schema and its contents from the database.
    • Code:
      DROP SCHEMA your_schema;