In Amazon Redshift a schema is a namespace inside a database that holds tables, views and functions. You create one with CREATE SCHEMA, control who can use it with GRANT, and choose which schemas unqualified names resolve to with search_path.
CREATE SCHEMA syntax
CREATE SCHEMA [ IF NOT EXISTS ] schema_name [ AUTHORIZATION username ] [ QUOTA { quota [ MB | GB | TB ] | UNLIMITED } ];Examples:
-- A schema for analytics modelsCREATE SCHEMA IF NOT EXISTS analytics;-- Owned by a specific user, with a storage quotaCREATE SCHEMA IF NOT EXISTS marketing AUTHORIZATION etl_user QUOTA 500 GB;IF NOT EXISTS makes the statement safe to re-run in deployment scripts. QUOTA caps the disk space the schema's tables can use; writes that exceed it fail.
Create tables in the schema
CREATE TABLE analytics.dim_customer ( customer_key BIGINT IDENTITY(1,1), customer_id VARCHAR(64) NOT NULL, name VARCHAR(256), region VARCHAR(64), PRIMARY KEY (customer_key))DISTSTYLE ALL;CREATE TABLE analytics.fct_orders ( order_id VARCHAR(64) NOT NULL, customer_key BIGINT NOT NULL REFERENCES analytics.dim_customer (customer_key), order_date DATE NOT NULL, net_amount DECIMAL(18,2) NOT NULL)DISTKEY (customer_key)SORTKEY (order_date);Redshift accepts primary and foreign keys but does not enforce them. The optimiser uses them as hints, so declare them only when they are true, and test uniqueness in your pipeline or dbt.
Grant access to a schema
Users need USAGE on the schema before they can access anything in it, plus privileges on the objects:
GRANT USAGE ON SCHEMA analytics TO GROUP analysts;GRANT SELECT ON ALL TABLES IN SCHEMA analytics TO GROUP analysts;-- Tables created later get the same grant automaticallyALTER DEFAULT PRIVILEGES IN SCHEMA analytics GRANT SELECT ON TABLES TO GROUP analysts;-- Allow a role to create objects in the schemaGRANT CREATE ON SCHEMA analytics TO etl_user;ALTER DEFAULT PRIVILEGES applies to objects created by the user who runs it (or the user named with FOR USER), so run it as the user that creates the tables.
Set the search path
Unqualified table names are resolved through search_path, which defaults to '$user', public:
SET search_path TO analytics, public; -- this sessionALTER USER analyst_1 SET search_path TO analytics, public; -- default for a userExternal schemas for Redshift Spectrum
An external schema points to a database in the AWS Glue Data Catalog, so you can query files in S3:
CREATE EXTERNAL SCHEMA IF NOT EXISTS lakeFROM DATA CATALOGDATABASE 'lake_db'IAM_ROLE 'arn:aws:iam::123456789012:role/redshift-spectrum'CREATE EXTERNAL DATABASE IF NOT EXISTS;List, rename and drop schemas
-- List schemas in the current databaseSELECT schema_name, schema_ownerFROM svv_all_schemasWHERE database_name = current_database();ALTER SCHEMA marketing RENAME TO mktg;DROP SCHEMA IF EXISTS staging_old; -- fails if it has objectsDROP SCHEMA IF EXISTS staging_old CASCADE; -- drops its objects tooOrganising schemas for a warehouse
A common layout is one schema per layer: raw or staging for loaded source data, intermediate for joined and cleaned models, and analytics or marts for the star schemas people query. Grant analysts access to the last one only.
Frequently asked questions
How do I create a schema in Redshift if it doesn't exist?
CREATE SCHEMA IF NOT EXISTS schema_name;
What is the difference between a database and a schema in Redshift?
A cluster or workgroup contains databases; a database contains schemas; a schema contains tables and views. Cross-database queries are possible with three-part names, but most warehouses use one database with several schemas.
Does Redshift enforce primary keys?
No. Primary, unique and foreign key constraints are informational only. Enforce them in your loading process or test them with dbt.
Generate Redshift DDL from a description
offdataai generates complete Redshift DDL, with schemas, distribution and sort keys, informational keys and a dbt project, from a plain-English description or one of 174 templates. See the Redshift schema generator.
