Bird
Raised Fist0
PostgreSQLquery~30 mins

Creating and switching schemas in PostgreSQL - Try It Yourself

Choose your learning style10 modes available
Creating and Switching Schemas in PostgreSQL
📖 Scenario: You are managing a PostgreSQL database for a small company. You want to organize data by creating separate schemas for different departments. This helps keep data tidy and easy to find.
🎯 Goal: Learn how to create a new schema and switch to it so you can create tables inside that schema.
📋 What You'll Learn
Create a new schema named sales
Switch the current schema search path to sales
Create a table named customers inside the sales schema
The customers table should have columns id (integer) and name (text)
💡 Why This Matters
🌍 Real World
Organizing database objects into schemas helps keep data separated by department or function, making management easier.
💼 Career
Database administrators and developers often create and switch schemas to manage complex databases efficiently.
Progress0 / 4 steps
1
Create the sales schema
Write a SQL statement to create a new schema called sales.
PostgreSQL
Hint

Use the CREATE SCHEMA command followed by the schema name.

2
Switch to the sales schema
Write a SQL statement to set the search path to the sales schema so that new tables will be created there by default.
PostgreSQL
Hint

Use the SET search_path TO command with the schema name.

3
Create the customers table in sales
Write a SQL statement to create a table named customers with columns id as integer and name as text. This table should be created inside the current schema, which is sales.
PostgreSQL
Hint

Use CREATE TABLE customers (id INTEGER, name TEXT); to create the table.

4
Verify the current schema search path
Write a SQL statement to show the current schema search path to confirm it is set to sales.
PostgreSQL
Hint

Use the SHOW search_path; command to display the current schema search path.

Practice

(1/5)
1. What is the main purpose of creating schemas in PostgreSQL?
easy
A. To encrypt the database tables
B. To speed up query execution automatically
C. To create backups of the database
D. To group database objects and organize them logically

Solution

  1. Step 1: Understand what schemas do

    Schemas help organize tables, views, and other objects into groups.
  2. Step 2: Identify the main purpose

    This organization helps keep the database tidy and manageable.
  3. Final Answer:

    To group database objects and organize them logically -> Option D
  4. Quick Check:

    Schemas = Organize objects [OK]
Hint: Schemas group objects to keep your database tidy [OK]
Common Mistakes:
  • Thinking schemas speed up queries automatically
  • Confusing schemas with backups
  • Assuming schemas encrypt data
2. Which of the following is the correct syntax to create a new schema named sales?
easy
A. NEW SCHEMA sales;
B. CREATE SCHEMA sales;
C. MAKE SCHEMA sales;
D. CREATE DATABASE sales;

Solution

  1. Step 1: Recall the command to create schemas

    The correct SQL command is CREATE SCHEMA schema_name;.
  2. Step 2: Match the syntax

    CREATE SCHEMA sales; matches this syntax exactly for schema sales.
  3. Final Answer:

    CREATE SCHEMA sales; -> Option B
  4. Quick Check:

    CREATE SCHEMA = create schema [OK]
Hint: Use CREATE SCHEMA followed by the schema name [OK]
Common Mistakes:
  • Using MAKE or NEW instead of CREATE
  • Confusing CREATE SCHEMA with CREATE DATABASE
  • Missing the semicolon at the end
3. Given the following commands:
CREATE SCHEMA marketing;
SET search_path TO marketing;
CREATE TABLE campaigns (id SERIAL PRIMARY KEY, name TEXT);

What schema will the table campaigns belong to?
medium
A. marketing
B. None, it will cause an error
C. default
D. public

Solution

  1. Step 1: Understand SET search_path effect

    Setting search_path to marketing means new objects are created in that schema by default.
  2. Step 2: Check where the table is created

    The CREATE TABLE campaigns command creates the table inside the current schema in search_path, which is marketing.
  3. Final Answer:

    marketing -> Option A
  4. Quick Check:

    SET search_path = schema for new objects [OK]
Hint: SET search_path sets default schema for new tables [OK]
Common Mistakes:
  • Assuming tables go to public schema by default
  • Thinking SET search_path only affects queries, not creation
  • Believing this causes an error without schema prefix
4. You run the command SET search_path TO sales; but get an error: ERROR: schema "sales" does not exist. What is the most likely cause?
medium
A. The schema sales was not created before setting search_path
B. You need to use USE sales; instead
C. The database connection is closed
D. The table sales does not exist

Solution

  1. Step 1: Understand error meaning

    The error says the schema sales does not exist, so PostgreSQL cannot set search_path to it.
  2. Step 2: Identify cause

    This usually means the schema was never created with CREATE SCHEMA sales;.
  3. Final Answer:

    The schema sales was not created before setting search_path -> Option A
  4. Quick Check:

    SET search_path needs existing schema [OK]
Hint: Create schema before setting search_path to it [OK]
Common Mistakes:
  • Using USE instead of SET search_path in PostgreSQL
  • Confusing tables with schemas
  • Ignoring the error and continuing
5. You have two schemas: public and archive. You want to query a table named orders in archive without typing the full name every time. Which command will let you switch your session to use archive schema first, then public?
hard
A. USE archive, public;
B. ALTER SCHEMA archive SET search_path;
C. SET search_path TO archive, public;
D. SET schema TO archive, public;

Solution

  1. Step 1: Recall how to set search_path for multiple schemas

    PostgreSQL uses SET search_path TO schema1, schema2; to set schema priority.
  2. Step 2: Identify correct syntax

    SET search_path TO archive, public; correctly sets archive first, then public.
  3. Final Answer:

    SET search_path TO archive, public; -> Option C
  4. Quick Check:

    SET search_path sets schema order [OK]
Hint: Use SET search_path with comma-separated schemas [OK]
Common Mistakes:
  • Using USE instead of SET search_path
  • Trying ALTER SCHEMA to set search_path
  • Using SET schema instead of SET search_path
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