CREATE SCHEMA groups tables, views, and other objects under a namespace, simplifying organization, security, and query performance tuning.
Separate schemas isolate business domains, simplify permission management, and keep query search paths short for faster planning. In an ecommerce cluster, store customer-facing tables in a public schema and analytics tables in a reporting schema.
CREATE SCHEMA builds a named namespace, optionally assigning an owner and granting default privileges. You can also create multiple objects inside a single statement, but most teams prefer one object per migration file for version control clarity.
Use lowercase, snake_case names like sales, analytics, or stage. Avoid generic names such as data or misc.
Grant ownership to a role like analytics_role, then grant USAGE on the schema to read-only roles. This enforces least privilege.
When you create tables inside the schema, pick DISTKEY and SORTKEY columns that match common joins and time filters to minimize data shuffling.
.
Reference the schema explicitly (sales.customers) or set SET search_path TO sales; before CREATE TABLE statements. Explicit references reduce ambiguity in multi-schema environments.
The example below creates a schema, grants access, and builds a table with proper keys.
BEGIN;
-- Schema
CREATE SCHEMA sales AUTHORIZATION analytics_role;
GRANT USAGE ON SCHEMA sales TO readonly_role;
-- Table
CREATE TABLE sales.customers (
id BIGINT IDENTITY(1,1),
name VARCHAR(100),
email VARCHAR(255),
created_at TIMESTAMP
)
DISTKEY(id)
SORTKEY(created_at);
COMMIT;
Iterate on your schema design during development sprints and monitor query plans to confirm that DIST/SORT choices remain optimal.
No. Schema creation is a metadata operation and completes in milliseconds without blocking running queries.
Yes. Use ALTER SCHEMA old_name RENAME TO new_name; but update application search_path settings afterward.
Run DROP SCHEMA sales CASCADE; after confirming that dependent objects can be recreated or are no longer needed.