Why do schemas matter in PostgreSQL?
Think about how you keep files organized in folders on your computer.
Schemas help group tables, views, and other objects logically. This prevents name clashes and helps manage permissions.
Given two schemas sales and marketing, each with a table named customers, what will this query return?
SELECT * FROM sales.customers;
Notice the schema name before the table name in the query.
Specifying the schema name before the table name tells PostgreSQL exactly which table to use, so it returns rows only from sales.customers.
Which option correctly creates a new schema named inventory in PostgreSQL?
Remember the SQL command to create schemas starts with CREATE SCHEMA.
The correct syntax to create a schema is CREATE SCHEMA schema_name;. Other options are invalid SQL commands.
How can you optimize queries to avoid always specifying the schema name before table names?
Think about how PostgreSQL decides which schema to look in first when you don't specify one.
Setting the search_path tells PostgreSQL which schemas to check first when resolving table names, so you can omit schema prefixes in queries.
You created a schema finance and a table reports inside it. When a user tries to query finance.reports, they get a permission denied error. What is the most likely cause?
Remember that schema permissions control access to objects inside them.
In PostgreSQL, users need USAGE privilege on a schema to access objects inside it, even if they have privileges on the tables.