187

I need to set schema path in Postgres so that I don't every time specify schema dot table e.g. schema2.table. Set schema path:

SET SCHEMA PATH a,b,c

only seems to work for one query session on mac, after I close query window the path variable sets itself back to default.

How can I make it permanent?

2
  • 2
    I think is SET search_path TO a, b,c; as the answer says and not SET SCHEMA PATH a,b,c; Commented Oct 24, 2019 at 18:44
  • There be dragons if you're using search_path: blog.bigsmoke.us/2022/11/11/postgresql-schema-search_path TL;DR: Don't use search_path in SECURITY DEFINER routines, make sure you control all schemas in the search_path, and also be careful when relying on the search_path within SECURITY INVOKER routines if these routines do things like generating a random password reset token.
    – BigSmoke
    Commented Nov 11, 2022 at 22:49

4 Answers 4

236

(And if you have no admin access to the server)

ALTER ROLE <your_login_role> SET search_path TO a,b,c;

Two important things to know about:

  1. When a schema name is not simple, it needs to be wrapped in double quotes.
  2. The order in which you set default schemas a, b, c matters, as it is also the order in which the schemas will be looked up for tables. So if you have the same table name in more than one schema among the defaults, there will be no ambiguity, the server will always use the table from the first schema you specified for your search_path.
6
  • 30
    Also worth noting to explicitly NOT use quotes around the a,b,c enumeration. Hand-to-forehead for the past 15 minutes...
    – Jmoney38
    Commented Mar 5, 2016 at 12:51
  • 7
    @Jmoney38 Not to use single quotes, but double-quotes are required for non-simple schema names.
    – vitaly-t
    Commented Apr 17, 2018 at 23:55
  • 1
    Yes, those are identifiers, not strings. Commented Aug 27, 2020 at 7:15
  • 2
    For me a reconnect to the database was necessary. SHOW SEARCH_PATH right after the ALTER ROLE seemed like it didn´t work at first.. Commented Apr 10, 2022 at 17:53
  • I don't recommend using this command because it will modify the search_path for all databases that the role has access on. I would advise using ALTER DATABASE <data_base> set search_path TO a,b,c; Commented Oct 10, 2022 at 11:42
191

You can set the default search_path at the database level:

ALTER DATABASE <database_name> SET search_path TO schema1,schema2;

Or at the user or role level:

ALTER ROLE <role_name> SET search_path TO schema1,schema2;

Or at the role+database level (thanks to Chris for pointing this out!):

ALTER ROLE <role_name> IN DATABASE <database_name> SET search_path TO schema1,schema2;

Or if you have a common default schema in all your databases you could set the system-wide default in the config file with the search_path option.

When a database is created it is created by default from a hidden "template" database named template1, you could alter that database to specify a new default search path for all databases created in the future. You could also create another template database and use CREATE DATABASE <database_name> TEMPLATE <template_name> to create your databases.

4
  • 12
    for those wondering from the psql command line you can list schemas by \dn
    – BenKoshy
    Commented Sep 30, 2016 at 1:00
  • 8
    Need to disconnect the session and connect again for the settings to take effect.
    – isapir
    Commented Dec 25, 2017 at 16:44
  • oh bless you. came over to PG from SQL Server so this is all new (and strange) to me Commented Sep 11, 2019 at 2:25
  • The documentation has it as user_name or role_name. It does not work when I create as role_name (groupname). It gives me the error as ERROR: permission denied for schema public. I granted usage and default to public and yet the permissions for role does not work. Any ideas or help on this please? Commented Nov 24, 2020 at 17:13
45

Josh is correct but he left out one variation:

ALTER ROLE <role_name> IN DATABASE <db_name> SET search_path TO schema1,schema2;

Set the search path for the user, in one particular database.

0
2

If you're working exclusively with one database on a machine, you can save a default schema to ~/.psqlrc:

set search_path to a,b,c

More information on that here. If you wanted to store default connection information so you can simply run psql each time, you'd need to use environmental variables for that:

export PGHOST=localhost
export PGPORT=5432
export PGDATABASE=database
export PGUSER=username

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.