Understanding Public Schema vs Custom Schemas in PostgreSQL
📖 Scenario: You are managing a PostgreSQL database for a small company. You want to organize your tables better by using schemas. By default, PostgreSQL uses the public schema, but you want to learn how to create and use a custom schema to keep your data organized.
🎯 Goal: Learn how to create a custom schema, create tables inside both the public schema and the custom schema, and understand how to query data from both schemas.
📋 What You'll Learn
Create a table in the default public schema
Create a custom schema named sales
Create a table inside the sales schema
Query data from both public and sales schemas
💡 Why This Matters
🌍 Real World
Organizing database objects into schemas helps keep data structured and easier to manage, especially in larger projects or multi-team environments.
💼 Career
Database administrators and developers often create and manage schemas to separate data logically, control access, and improve clarity in complex databases.
Progress0 / 4 steps
1
Create a table in the public schema
Write a SQL statement to create a table called customers in the default public schema with columns id (integer primary key) and name (text).
PostgreSQL
Hint
Use CREATE TABLE public.customers to specify the table in the public schema.
2
Create a custom schema named sales
Write a SQL statement to create a new schema called sales in the database.
PostgreSQL
Hint
Use CREATE SCHEMA sales; to create the custom schema.
3
Create a table inside the sales schema
Write a SQL statement to create a table called orders inside the sales schema with columns order_id (integer primary key) and customer_id (integer).
PostgreSQL
Hint
Use CREATE TABLE sales.orders to create the table inside the sales schema.
4
Query data from both schemas
Write two SQL SELECT statements: one to select all columns from customers in the public schema, and one to select all columns from orders in the sales schema.
PostgreSQL
Hint
Use SELECT * FROM public.customers; and SELECT * FROM sales.orders; to query both tables.
Practice
(1/5)
1. What is the role of the public schema in PostgreSQL?
easy
A. It is a special schema that cannot contain tables or other objects.
B. It is the default schema where database objects are created if no schema is specified.
C. It is used only for system tables and cannot be modified by users.
D. It automatically encrypts all data stored within it.
Solution
Step 1: Understand the default schema concept
In PostgreSQL, the public schema is created by default in every new database.
Step 2: Identify the role of the public schema
When you create tables or other objects without specifying a schema, they go into the public schema automatically.
Final Answer:
It is the default schema where database objects are created if no schema is specified. -> Option B
Quick Check:
Default schema = public [OK]
Hint: Remember: public schema is default for new objects [OK]
Common Mistakes:
Thinking public schema is read-only
Assuming public schema is for system tables only
Believing public schema encrypts data automatically
2. Which of the following is the correct way to create a new schema named sales in PostgreSQL?
easy
A. CREATE SCHEMA sales;
B. CREATE DATABASE sales;
C. NEW SCHEMA sales;
D. MAKE SCHEMA sales;
Solution
Step 1: Recall the SQL syntax for creating schemas
The correct SQL command to create a schema is CREATE SCHEMA schema_name;.
Step 2: Check each option
CREATE SCHEMA sales; uses the correct syntax. MAKE SCHEMA sales;, CREATE DATABASE sales;, and NEW SCHEMA sales; are invalid SQL commands for creating schemas.
Final Answer:
CREATE SCHEMA sales; -> Option A
Quick Check:
CREATE SCHEMA = create new schema [OK]
Hint: Use CREATE SCHEMA to make new schemas [OK]
Common Mistakes:
Using CREATE DATABASE instead of CREATE SCHEMA
Using non-existent commands like NEW SCHEMA or MAKE SCHEMA
Forgetting the semicolon at the end
3. Given the following commands:
CREATE SCHEMA marketing;
CREATE TABLE marketing.campaigns (id SERIAL PRIMARY KEY, name TEXT);
SET search_path TO marketing, public;
SELECT * FROM campaigns;
What will be the result of the SELECT query?
medium
A. It will return rows from a campaigns table in the public schema if it exists.
B. It will cause an error because the table name is not schema-qualified.
C. It will return all rows from the marketing.campaigns table.
D. It will return no rows because search_path is set incorrectly.
Solution
Step 1: Understand search_path usage
The search_path is set to look first in marketing schema, then public.
Step 2: Identify which table is found
The table campaigns exists in marketing schema, so the query finds and returns data from marketing.campaigns.
Final Answer:
It will return all rows from the marketing.campaigns table. -> Option C
Quick Check:
search_path order finds marketing first [OK]
Hint: search_path order decides which schema is used first [OK]
Common Mistakes:
Assuming table must be schema-qualified always
Thinking query errors without schema prefix
Ignoring search_path order
4. You run this command:
SELECT * FROM sales.orders;
But get the error: ERROR: schema "sales" does not exist. What is the most likely cause?
medium
A. The sales schema has not been created yet.
B. The orders table does not exist in the public schema.
C. You forgot to set the search_path to include sales.
D. You need to use double quotes around sales.
Solution
Step 1: Analyze the error message
The error says the schema "sales" does not exist, meaning PostgreSQL cannot find that schema.
Step 2: Identify the cause
This usually means the schema was never created or was dropped, so the database has no knowledge of it.
Final Answer:
The sales schema has not been created yet. -> Option A
Quick Check:
Schema missing causes this error [OK]
Hint: Check if schema exists before querying it [OK]
Common Mistakes:
Assuming search_path affects schema existence errors
Thinking table absence causes schema not found error
Trying to fix with quotes unnecessarily
5. You want to organize your database by creating two schemas: hr and finance. You create tables with the same name employees in both schemas. How can you write a query to select all employees from the finance schema without changing the search_path?
hard
A. SELECT * FROM public.employees WHERE schema = 'finance';
B. SET search_path TO finance; SELECT * FROM employees;
C. SELECT * FROM employees WHERE schema = 'finance';
D. SELECT * FROM finance.employees;
Solution
Step 1: Understand schema-qualified table names
When tables have the same name in different schemas, you can specify the schema explicitly before the table name.
Step 2: Identify the correct query
SELECT * FROM finance.employees; uses finance.employees which directly accesses the table in the finance schema without changing search_path.