AI Hints
Home›PostgreSQL Tutorial›Views and Schemas
PostgreSQL · Lesson 72 of 120

Schemas and search_path

Folders for tables.

⏱ 2 min read▶ Video at 1:04:52🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

As a database grows, tables multiply. Schemas group them, like folders. Reporting tables in one schema, raw imports in another.

  • Many tables
  • Group them
  • Schemas

A reporting schema

Create schema makes a folder called reports. Tables inside it are named reports dot something. Search path decides which schemas Postgres looks in, when you don't say. Run it. The table is found both ways.

query.sqlsql
CREATE SCHEMA reports;
CREATE TABLE reports.daily (day date, orders int);
INSERT INTO reports.daily VALUES ('2025-01-01', 3);

SELECT * FROM reports.daily;

SET search_path = reports, public;
SELECT * FROM daily;
Output
CREATE SCHEMA
CREATE TABLE
INSERT 0 1
    day     | orders
------------+--------
 2025-01-01 |      3
(1 row)

SET
    day     | orders
------------+--------
 2025-01-01 |      3
(1 row)

Schema tips

Public is the default schema. Use schemas to separate apps, reports and imports. Grant permissions per schema. And keep the search path simple, so names are never ambiguous.

keyvalue
publicdefault
Separateapps, reports, imports
GRANTper schema
search_pathkeep it simple

Summary

Schemas group tables like folders, and the search path decides where Postgres looks. Next, writing your own functions.

Key takeaways
  • CREATE SCHEMA
  • schema.table
  • search_path
Next up: SQL Functions — reusable logic