What if your database was as messy as a junk drawer--how would you find anything?
Public schema vs custom schemas in PostgreSQL - When to Use Which
Imagine you have a big filing cabinet where everyone throws their papers into the same drawer without any order.
Finding a specific document becomes a frustrating hunt through a messy pile.
When all database objects live in the public schema, it's like that messy drawer.
It's hard to organize, easy to overwrite others' work, and difficult to manage permissions.
This slows down development and causes mistakes.
Using custom schemas is like having separate labeled drawers for different projects or teams.
It keeps things tidy, avoids conflicts, and lets you control who can see or change what.
CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT); -- all in public schemaCREATE SCHEMA sales; CREATE TABLE sales.customers (id SERIAL PRIMARY KEY, name TEXT);
Custom schemas enable clear organization and secure separation of database objects, making teamwork smoother and safer.
A company uses a public schema for general data but creates custom schemas for each department like HR, Sales, and Finance to keep their data separate and secure.
Public schema is the default shared space for database objects.
Custom schemas let you group and separate objects logically.
This improves organization, security, and collaboration.
Practice
public schema in PostgreSQL?Solution
Step 1: Understand the default schema concept
In PostgreSQL, thepublicschema 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 thepublicschema automatically.Final Answer:
It is the default schema where database objects are created if no schema is specified. -> Option BQuick Check:
Default schema = public [OK]
- Thinking public schema is read-only
- Assuming public schema is for system tables only
- Believing public schema encrypts data automatically
sales in PostgreSQL?Solution
Step 1: Recall the SQL syntax for creating schemas
The correct SQL command to create a schema isCREATE 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 AQuick Check:
CREATE SCHEMA = create new schema [OK]
- Using CREATE DATABASE instead of CREATE SCHEMA
- Using non-existent commands like NEW SCHEMA or MAKE SCHEMA
- Forgetting the semicolon at the end
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?
Solution
Step 1: Understand search_path usage
Thesearch_pathis set to look first inmarketingschema, thenpublic.Step 2: Identify which table is found
The tablecampaignsexists inmarketingschema, so the query finds and returns data frommarketing.campaigns.Final Answer:
It will return all rows from the marketing.campaigns table. -> Option CQuick Check:
search_path order finds marketing first [OK]
- Assuming table must be schema-qualified always
- Thinking query errors without schema prefix
- Ignoring search_path order
SELECT * FROM sales.orders;
But get the error:
ERROR: schema "sales" does not exist.What is the most likely cause?
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 AQuick Check:
Schema missing causes this error [OK]
- Assuming search_path affects schema existence errors
- Thinking table absence causes schema not found error
- Trying to fix with quotes unnecessarily
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?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; usesfinance.employeeswhich directly accesses the table in the finance schema without changing search_path.Final Answer:
SELECT * FROM finance.employees; -> Option DQuick Check:
Schema-qualified names access tables directly [OK]
- Trying to filter by schema column which doesn't exist
- Changing search_path unnecessarily
- Assuming public schema contains all tables
Ready to prove it?
Pass the test and this pattern moves unknown
