Bird
Raised Fist0
PostgreSQLquery~5 mins

Creating and switching schemas in PostgreSQL

Choose your learning style10 modes available
Introduction

Schemas help organize database objects like tables into groups. Creating and switching schemas lets you keep things tidy and work in the right group.

You want to separate data for different projects in the same database.
You need to organize tables by department, like sales and marketing.
You want to avoid name conflicts by putting tables in different schemas.
You want to control access by giving permissions on specific schemas.
You want to switch your work context to a different schema easily.
Syntax
PostgreSQL
CREATE SCHEMA schema_name;

SET search_path TO schema_name;

CREATE SCHEMA makes a new schema with the name you choose.

SET search_path changes your current schema so you can use objects there without full names.

Examples
This creates a new schema called sales.
PostgreSQL
CREATE SCHEMA sales;
This switches your current schema to sales. Now you can create or access tables there without prefixing sales..
PostgreSQL
SET search_path TO sales;
Create a marketing schema and switch to it immediately.
PostgreSQL
CREATE SCHEMA marketing;
SET search_path TO marketing;
Sample Program

This example creates a schema named test_schema, switches to it, creates a customers table, inserts two rows, and then selects all rows.

PostgreSQL
CREATE SCHEMA test_schema;
SET search_path TO test_schema;
CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT);
INSERT INTO customers (name) VALUES ('Alice'), ('Bob');
SELECT * FROM customers;
OutputSuccess
Important Notes

When you switch schemas with SET search_path, you don't need to write the schema name before table names.

If you don't set the search path, PostgreSQL uses the default public schema.

Be careful: switching schemas only affects your current database session.

Summary

Schemas group database objects to keep things organized.

Create schemas with CREATE SCHEMA and switch using SET search_path.

Switching schemas helps you work in the right context without typing full names.

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