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
CREATE SCHEMA [ IF NOT EXISTS ] schema_name;
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:
CREATE DATABASE company;
CREATE DATABASE
\c company
You are now connected to database "company" as user "postgres".
Step 2: Create the Schema
CREATE SCHEMA sales;
CREATE SCHEMA
Step 3: Create a Table in the Schema
CREATE TABLE sales.orders (item 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:
INSERT INTO sales.orders (item) VALUES ('Desk');
INSERT 0 1
SELECT * FROM sales.orders;
item
------
Desk
(1 row)
Step 4: List the Schema’s Tables
\dt sales.*
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
SELECT * FROM orders;
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:
CREATE SCHEMA sales;
ERROR: schema "sales" already exists
IF NOT EXISTS turns the error into a notice, which helps in a script that may run twice:
CREATE SCHEMA IF NOT EXISTS sales;
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE company;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the schema name | ERROR: relation "orders" does not exist | Write sales.orders, or put sales on the search path. |
| Creating a schema that already exists | ERROR: schema "sales" already exists | Use 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
- PostgreSQL 19 documentation: CREATE SCHEMA (opens in a new tab)
- PostgreSQL 19 documentation: Schemas (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Set the search_path in PostgreSQL
- How to List Tables in PostgreSQL
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