Skip to main content
0 reactions

How to Create a Schema in PostgreSQL

Featured card for How to Create a Schema in PostgreSQL, with a terminal showing CREATE SCHEMA sales, the table sales.orders, its listing, and IF NOT EXISTS.
How to Create a Schema in PostgreSQL

To create a schema in PostgreSQL, use CREATE SCHEMA with a name. A schema is a folder inside a database that groups tables, and the same table name can exist in two schemas without a clash. Here’s how to create one and put a table in it.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
CREATE SCHEMA [ IF NOT EXISTS ] schema_name;
SyntaxSQL
schema_name.table_name
  • schema_name: the new schema’s name.
  • IF NOT EXISTS: optional. If the schema is already there, skip it with a notice instead of an error.
  • schema_name.table_name: a qualified name, the schema and the table joined by a dot. It works wherever a table name does.
  • [ ] marks the optional part.

See CREATE SCHEMA (opens in a new tab) and Schemas (opens in a new tab) in the PostgreSQL documentation.

The Example

A company keeps its sales tables apart from everything else, in a schema named sales.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE company;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c company
OutputPlain text
You are now connected to database "company" as user "postgres".

Step 2: Create the Schema

company=#SQL
CREATE SCHEMA sales;
OutputPlain text
CREATE SCHEMA

Step 3: Create a Table in the Schema

company=#SQL
CREATE TABLE sales.orders (item text);
OutputPlain text
CREATE TABLE

sales. in front of the name puts the table in sales instead of public. Use the same full name to add and read rows:

company=#SQL
INSERT INTO sales.orders (item) VALUES ('Desk');
OutputPlain text
INSERT 0 1
company=#SQL
SELECT * FROM sales.orders;
OutputPlain text
 item
------
 Desk
(1 row)

Step 4: List the Schema’s Tables

company=#Plain text
\dt sales.*
OutputPlain text
           List of tables
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 sales  | orders | table | postgres
(1 row)

The Schema column shows sales. How to List Tables in PostgreSQL explains the pattern.

Step 5: Try the Short Name

company=#SQL
SELECT * FROM orders;
OutputPlain text
ERROR:  relation "orders" does not exist
LINE 1: SELECT * FROM orders;
                      ^

PostgreSQL looks for a name without a schema only in the schemas on its search path, which starts as "$user", public. Write sales.orders, or add sales to the path as How to Set the search_path in PostgreSQL shows.

Step 6: Create a Schema Only If It’s Missing

Creating sales again fails, because it exists:

company=#SQL
CREATE SCHEMA sales;
OutputPlain text
ERROR:  schema "sales" already exists

IF NOT EXISTS turns the error into a notice, which helps in a script that may run twice:

company=#SQL
CREATE SCHEMA IF NOT EXISTS sales;
OutputPlain text
NOTICE:  schema "sales" already exists, skipping
CREATE SCHEMA

Clean Up

To remove the example, connect to another database and drop the test database, which removes the schema and its table too:

company=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE company;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Leaving out the schema nameERROR: relation "orders" does not existWrite sales.orders, or put sales on the search path.
Creating a schema that already existsERROR: schema "sales" already existsUse CREATE SCHEMA IF NOT EXISTS.

FAQ

What is the public schema?

Every new database has a schema called public, and tables go there unless you name another schema.

Are schemas like databases?

Not quite. A database is a separate world: a connection uses one database at a time. Schemas are folders inside one database, and a query can use tables from several schemas at once.

Conclusion

CREATE SCHEMA makes a folder for tables inside a database. Create a table in it with a qualified name such as sales.orders, and use that name, or the search path, to reach it.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments