Skip to main content
0 reactions

How to Set the search_path in PostgreSQL

Featured card for How to Set the search_path in PostgreSQL, with a terminal showing SHOW search_path returning the user schema and public, then SET search_path TO art, public.
How to Set the search_path in PostgreSQL

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

SyntaxSQL
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:

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

Step 2: Create a Schema and a Table

museum=#SQL
CREATE SCHEMA art;
OutputPlain text
CREATE SCHEMA
museum=#SQL
CREATE TABLE art.paintings (title text);
OutputPlain text
CREATE TABLE
museum=#SQL
INSERT INTO art.paintings (title) VALUES ('Harbour at Dusk');
OutputPlain text
INSERT 0 1

Step 3: See the Current Path

museum=#SQL
SHOW search_path;
OutputPlain text
   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:

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

Step 4: Change the Path for Your Session

museum=#SQL
SET search_path TO art, public;
OutputPlain text
SET
museum=#SQL
SHOW search_path;
OutputPlain text
 search_path
-------------
 art, public
(1 row)
museum=#SQL
SELECT * FROM paintings;
OutputPlain text
      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:

museum=#Plain text
\c museum
OutputPlain text
You are now connected to database "museum" as user "postgres".
museum=#SQL
SHOW search_path;
OutputPlain text
   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

museum=#SQL
ALTER DATABASE museum SET search_path TO art, public;
OutputPlain text
ALTER DATABASE
museum=#Plain text
\c museum
OutputPlain text
You are now connected to database "museum" as user "postgres".
museum=#SQL
SHOW search_path;
OutputPlain text
 search_path
-------------
 art, public
(1 row)
museum=#SQL
SELECT * FROM paintings;
OutputPlain text
      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:

museum=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE museum;
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
Expecting SET search_path to lastA 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 pathTables 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 pathERROR: relation "paintings" does not existAdd 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

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