0
0
PostgreSQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - Public schema vs custom schemas
Start: Connect to DB
Check default schema
Public schema exists?
YesUse 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.