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.
search_path
: blog.bigsmoke.us/2022/11/11/postgresql-schema-search_path TL;DR: Don't usesearch_path
inSECURITY DEFINER
routines, make sure you control all schemas in thesearch_path
, and also be careful when relying on thesearch_path
withinSECURITY INVOKER
routines if these routines do things like generating a random password reset token.