Skip to content
Databasesbeginner

What is a database schema?

Updated Nov 30, 20263 min readBased on official documentation
What is a database schema?

In short

A database schema is a named group inside a database that holds tables and other objects such as functions and data types. It helps keep related objects organized and prevents name clashes. In PostgreSQL, you create one with the CREATE SCHEMA command.

What a schema is

A database is a container for your data. Inside it, you can have one or more schemas. Think of a schema as a named folder. It holds tables, and it can also hold other named objects such as data types, functions, and operators.

The documentation for PostgreSQL describes this structure clearly. A server can run several databases, and each database can hold several schemas. A client connection works with one database at a time, but within that database it can reach objects in any schema it has permission to use.

Why use schemas

Schemas solve a few common problems. First, they let many users share one database without stepping on each other's work. Each team or user can have a schema of their own. Second, they help you group related objects so the database is easier to manage. Third, they protect against name clashes. A third-party application can live in its own schema, so its table names will not collide with yours.

There is one rule to remember. Inside a single schema, two objects of the same kind cannot share a name. Tables, views, indexes and sequences also share one namespace, so an index and a table in the same schema cannot have the same name. However, two different schemas can each contain a table called the same thing.

A simple example

To create a schema, run the CREATE SCHEMA command and pick a name:

CREATE SCHEMA myschema;
CREATE TABLE myschema.mytable (
    id integer,
    name text
);
DROP SCHEMA myschema CASCADE;

The first line creates the schema. The second creates a table inside it. You refer to that table with a qualified name: the schema name, a dot, then the table name. The last line drops the schema and everything in it. Without CASCADE, PostgreSQL only drops a schema that is already empty.

If you leave out the schema name, PostgreSQL puts the table in a schema called public. Every new database has this schema by default.

How the search path works

Writing full qualified names every time is tedious. So most queries use plain table names. PostgreSQL then looks through a list of schemas called the search path, in order, and uses the first match. You can see the current value with SHOW search_path;. By default it includes a schema named after the current user, followed by public.

You can change it for your session. For example, SET search_path TO myschema,public; makes myschema the first place to look and also the place where new tables are created by default.

Common mistakes

A frequent surprise is an unqualified name that resolves to the wrong table. If two schemas on the search path both have a table with the same name, the one listed first wins. Check the search path when a query returns data you did not expect.

Another point is security. Anyone who can create objects in a schema on your search path may be able to affect your queries. Keep the search path short and avoid giving broad create rights on shared schemas.

Finally, names beginning with pg_ are reserved for PostgreSQL's own system objects, so you cannot use them for your schemas.

A schema is not the same as a database. A database is a separate container. Objects in one database cannot be reached from a connection to another database in the same way that schemas within one database can be reached. If you need strong separation between projects, separate databases may fit better. If you only need organization inside one project, schemas are usually enough.

For the full reference, read the official PostgreSQL documentation on schemas linked from this page.

FAQ

Is a schema the same as a database?

No. A database holds one or more schemas, and each schema holds tables and other named objects. Schemas inside one database are easier to share than separate databases.

What happens if I do not name a schema when creating a table?

In PostgreSQL the table is placed in a schema called public. Every new database includes that schema by default.

Why does my query pick up the wrong table?

Unqualified table names are resolved using the search path. The first schema in that list containing a matching table wins, so check the output of SHOW search_path.

Can I drop a schema that still has tables in it?

Not with a plain DROP SCHEMA, which only works on an empty schema. Using DROP SCHEMA with CASCADE removes the schema along with all its contained objects, so use it carefully.

Sources

This page is based on the official documentation below. Read it for full detail.

Database schema — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.