Bird
Raised Fist0
PostgreSQLquery~10 mins

Public schema vs custom schemas in PostgreSQL - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Public schema vs custom schemas
Start: Connect to DB
↓
Check default schema
↓
Public schema exists?
Yes→Use public schema
↓
Create custom schema
↓
Create public schema
↓
Use custom schema
↓
Create objects in schema
↓
Query objects with schema prefix or search_path
↓
End
This flow shows how PostgreSQL uses the public schema by default, but you can create and use custom schemas to organize database objects separately.
Execution Sample
PostgreSQL
CREATE SCHEMA sales;
CREATE TABLE sales.orders (id SERIAL PRIMARY KEY, amount INT);
INSERT INTO sales.orders (amount) VALUES (100);
SELECT * FROM sales.orders;
This code creates a custom schema 'sales', adds a table 'orders' inside it, inserts a row, and selects data from it.
Execution Table
StepActionSchema AffectedObject Created/ModifiedResult/Output
1CREATE SCHEMA sales;salesSchema 'sales' createdSuccess
2CREATE TABLE sales.orders (id SERIAL PRIMARY KEY, amount INT);salesTable 'orders' createdSuccess
3INSERT INTO sales.orders (amount) VALUES (100);salesRow inserted into 'orders'1 row affected
4SELECT * FROM sales.orders;salesQuery executed[{id:1, amount:100}]
5SELECT * FROM orders;publicQuery executedError: relation "orders" does not exist
6SET search_path TO sales;sessionSearch path changedSuccess
7SELECT * FROM orders;salesQuery executed[{id:1, amount:100}]
💡 Execution stops after successful queries and error when querying non-existent table in public schema.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
search_pathpublicpublicpublicpublicpublicpublicsalessales
schemaspublicpublic, salespublic, salespublic, salespublic, salespublic, salespublic, salespublic, sales
tables in salesnonenoneordersordersordersordersordersorders
tables in publicnonenonenonenonenonenonenonenone
Key Moments - 2 Insights
Why does 'SELECT * FROM orders;' fail before setting search_path to 'sales'?
Because by default, PostgreSQL looks in the 'public' schema, which has no 'orders' table. The 'orders' table exists only in the 'sales' schema, so the query fails until search_path includes 'sales' (see execution_table step 5 and 7).
What happens when you create a table without specifying a schema?
It is created in the 'public' schema by default. This is why 'public' is the default schema unless you specify another or change the search_path.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What data is returned by 'SELECT * FROM sales.orders;'?
AEmpty result
B[{id:1, amount:100}]
CError: relation does not exist
D[{id:0, amount:0}]
💡 Hint
Check the 'Result/Output' column for step 4 in the execution_table.
At which step does the search_path change to include the 'sales' schema?
AStep 6
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Action' and 'Schema Affected' columns in execution_table for when search_path changes.
If you run 'SELECT * FROM orders;' without setting search_path to 'sales', what happens?
AReturns rows from sales.orders
BReturns empty result
CError: relation 'orders' does not exist
DReturns rows from public.orders
💡 Hint
See execution_table step 5 for the query without schema prefix or search_path change.
Concept Snapshot
PostgreSQL uses 'public' schema by default.
Custom schemas organize objects separately.
Create schema with CREATE SCHEMA name;
Use schema-qualified names: schema.table
Change search_path to set default schema order.
Queries look in schemas by search_path order.
Full Transcript
In PostgreSQL, the default schema is 'public'. When you create tables without specifying a schema, they go into 'public'. You can create custom schemas to organize your tables and other objects separately. For example, creating a 'sales' schema and putting an 'orders' table inside it. By default, queries look in the 'public' schema unless you specify the schema name or change the search_path. If you query a table without schema prefix and it doesn't exist in 'public', you get an error. Changing the search_path to include your custom schema lets you query tables there without prefix. This helps keep database objects organized and avoid name conflicts.

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

  1. Step 1: Understand the default schema concept

    In PostgreSQL, the public schema is created by default in every new database.
  2. 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.
  3. Final Answer:

    It is the default schema where database objects are created if no schema is specified. -> Option B
  4. 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

  1. Step 1: Recall the SQL syntax for creating schemas

    The correct SQL command to create a schema is CREATE SCHEMA schema_name;.
  2. 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.
  3. Final Answer:

    CREATE SCHEMA sales; -> Option A
  4. 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

  1. Step 1: Understand search_path usage

    The search_path is set to look first in marketing schema, then public.
  2. Step 2: Identify which table is found

    The table campaigns exists in marketing schema, so the query finds and returns data from marketing.campaigns.
  3. Final Answer:

    It will return all rows from the marketing.campaigns table. -> Option C
  4. 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

  1. Step 1: Analyze the error message

    The error says the schema "sales" does not exist, meaning PostgreSQL cannot find that schema.
  2. Step 2: Identify the cause

    This usually means the schema was never created or was dropped, so the database has no knowledge of it.
  3. Final Answer:

    The sales schema has not been created yet. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    SELECT * FROM finance.employees; -> Option D
  4. Quick Check:

    Schema-qualified names access tables directly [OK]
Hint: Use schema.table to access specific schema tables [OK]
Common Mistakes:
  • Trying to filter by schema column which doesn't exist
  • Changing search_path unnecessarily
  • Assuming public schema contains all tables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard