To set the search_path in PostgreSQL, use SET search_path TO and a list of schemas. The search path tells PostgreSQL which schemas to look in for a table you name without its schema. Here’s how to see it, change it for your session, and change it for every new session in a database.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
SHOW search_path;
SET search_path TO schema_name [, ...];
ALTER DATABASE database_name SET search_path TO schema_name [, ...];
SHOW search_path: shows the current path.SET search_path TO: changes it for your session only. List the schemas in the order PostgreSQL should look in them, separated by commas.ALTER DATABASE … SET search_path TO: makes it the starting path of every new session in that database.[, ...]means you can list more schemas.
See The Schema Search Path (opens in a new tab) and search_path (opens in a new tab) in the PostgreSQL documentation.
The Example
A museum keeps its art in a schema named art, with a paintings table. You want to write paintings instead of art.paintings.
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 museum;
CREATE DATABASE
\c museum
You are now connected to database "museum" as user "postgres".
Step 2: Create a Schema and a Table
CREATE SCHEMA art;
CREATE SCHEMA
CREATE TABLE art.paintings (title text);
CREATE TABLE
INSERT INTO art.paintings (title) VALUES ('Harbour at Dusk');
INSERT 0 1
Step 3: See the Current Path
SHOW search_path;
search_path
-----------------
"$user", public
(1 row)
"$user" stands for a schema with the same name as your user. There’s none here, so PostgreSQL skips it and looks in public. That’s why the short name fails:
SELECT * FROM paintings;
ERROR: relation "paintings" does not exist
LINE 1: SELECT * FROM paintings;
^
Step 4: Change the Path for Your Session
SET search_path TO art, public;
SET
SHOW search_path;
search_path
-------------
art, public
(1 row)
SELECT * FROM paintings;
title
-----------------
Harbour at Dusk
(1 row)
Now the short name works. public stays on the list, so its tables still work by their short names too.
Step 5: See That SET Lasts One Session
Connect to the database again, which starts a new session:
\c museum
You are now connected to database "museum" as user "postgres".
SHOW search_path;
search_path
-----------------
"$user", public
(1 row)
The path is back to its default: SET only changed the old session.
Step 6: Set the Path for the Whole Database
ALTER DATABASE museum SET search_path TO art, public;
ALTER DATABASE
\c museum
You are now connected to database "museum" as user "postgres".
SHOW search_path;
search_path
-------------
art, public
(1 row)
SELECT * FROM paintings;
title
-----------------
Harbour at Dusk
(1 row)
Every new session in museum now starts with art, public. Sessions that were already open keep their path until they reconnect.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE museum;
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 |
|---|---|---|
Expecting SET search_path to last | A new session starts with the default path again, as Step 5 showed. | Use ALTER DATABASE … SET search_path, as in Step 6. |
Leaving public out of the path | Tables in public then need their full name, such as public.table_name. | Keep public in the list, after your own schemas. |
| Using a short name for a schema that isn’t on the path | ERROR: relation "paintings" does not exist | Add the schema to the path, or write its name in front of the table. |
FAQ
Where do new tables go?
Into the first schema on the path that exists. With art, public, a CREATE TABLE without a schema name goes into art, as the documentation (opens in a new tab) explains.
Can I set the path for one user instead of a database?
Yes. ALTER ROLE (opens in a new tab) takes the same SET search_path TO …, and it applies to every new session of that user.
Conclusion
SHOW search_path shows where PostgreSQL looks for tables, SET search_path TO changes it for your session, and ALTER DATABASE … SET search_path TO makes it the default for every new session in a database.
Additional Resources
- PostgreSQL 19 documentation: The Schema Search Path (opens in a new tab)
- PostgreSQL 19 documentation: search_path (opens in a new tab)
- PostgreSQL 19 documentation: ALTER DATABASE (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Create a Schema 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