WikiGalaxy

Personalize

PostgreSQL Create Schema

Introduction:

In PostgreSQL, a schema is a namespace that contains database objects such as tables, views, functions, etc. Creating a schema allows you to organize these objects into logical groups.

Basic Syntax:

The basic syntax for creating a schema in PostgreSQL is:


CREATE SCHEMA schema_name;
      

Example:

Let's create a schema named sales:


CREATE SCHEMA sales;
      

Schema Ownership:

You can specify an owner for the schema using the AUTHORIZATION clause:


CREATE SCHEMA sales AUTHORIZATION manager;
      

Using Schemas:

Once a schema is created, you can create tables and other objects within it:


CREATE TABLE sales.orders (
    order_id SERIAL PRIMARY KEY,
    order_date DATE NOT NULL
);
      

Search Path:

PostgreSQL uses a search path to resolve unqualified object names. You can set the search path to include your schema:


SET search_path TO sales, public;
      

Managing Schemas:

To delete a schema, use the DROP SCHEMA command. Note that this will remove all objects within the schema:


DROP SCHEMA sales CASCADE;
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025