PostgreSQL Schema
Summary: in this tutorial, you will learn about PostgreSQL schema and how to use the schema search path to resolve objects in schemas.
Introduction to PostgreSQL schema
In PostgreSQL, a schema is a named collection of database objects, including tables, views, indexes, data types, functions, stored procedures, and operators.
A schema allows you to organize and namespace database objects within a database.
To access an object in a schema, you need to qualify the object by using the following syntax:
A database may contain one or more schemas. However, a schema belongs to only one database. Additionally, two schemas can have different objects that share the same name.
For example, you may have sales
schema that has staff
table and the public
schema which also has the staff
table. When you refer to the staff
table you must qualify it as follows:
Or
Schemas can be very useful in the following scenarios:
- Schemas allow you to organize database objects e.g., tables into logical groups to make them more manageable.
- Schemas enable multiple users to use one database without interfering with each other.
The public schema
PostgreSQL automatically creates a schema called public
for every new database. Whatever object you create without specifying the schema name, PostgreSQL will place it into this public
schema. Therefore, the following statements are equivalent:
and
The schema search path
In practice, you will refer to a table without its schema name e.g., staff
table instead of a fully qualified name such as sales.staff
table.
When you reference a table using its name only, PostgreSQL searches for the table by using the schema search path, which is a list of schemas to look in.
PostgreSQL will access the first matching table in the schema search path. If there is no match, it will return an error, even if the name exists in another schema in the database.
The first schema in the search path is called the current schema. Note that when you create a new object without explicitly specifying a schema name, PostgreSQL will also use the current schema for the new object.
The current_schema()
function returns the current schema:
Here is the output:
This is why PostgreSQL uses public
for every new object that you create.
To view the current search path, you use the SHOW
command in psql
tool:
The output is as follows:
In this output:
- The
"$user"
specifies that the first schema that PostgreSQL will use to search for the object, which has the same name as the current user. For example, if you use thepostgres
user to log in and access thestaff
table. PostgreSQL will search for thestaff
table in thepostgres
schema. If it cannot find any object like that, it continues to look for the object in thepublic
schema. - The second element refers to the
public
schema as we have seen before.
To create a new schema, you use the CREATE SCHEMA
statement:
To add the new schema to the search path, you use the following command:
Now, if you create a new table named staff
without specifying the schema name, PostgreSQL will put this staff
table into the sales
schema:
The following picture shows the new schema sales
and the staff
table that belongs to the sales
schema:
To access the staff
table in the sales
schema you can use one of the following statements:
and
The public
schema is the second element in the search path, so to access the staff
table in the public schema, you must qualify the table name as follows:
If you use the following command, you will need to explicitly refer to objects in the public
schema using a fully qualified name:
The public
schema is not a special schema, therefore, you can drop it too.
PostgreSQL schemas and privileges
Users can only access objects in the schemas that they own. It means they cannot access any objects in the schemas that do not belong to them.
To allow users to access the objects in the schema that they do not own, you must grant the USAGE
privilege of the schema to the users:
To allow users to create objects in the schema that they do not own, you need to grant them the CREATE
privilege of the schema to the users:
Note that, by default, every user has the CREATE
and USAGE
on the public
schema.
PostgreSQL schema operations
- To create a new schema, you use the
CREATE SCHEMA
statement. - To rename a schema or change its owner, you use the
ALTER SCHEMA
statement. - To drop a schema, you use the
DROP SCHEMA
statement.
Summary
- A schema is a named collection of database objects, including tables, views, indexes, sequences, and so on.
- Use schemas to organize and namespace these objects within a database.
- Use the search path to resolve object names.