Created
April 2, 2023 13:14
-
-
Save jianhe-fun/3c8f560d35e95903bdeb82c0b6325879 to your computer and use it in GitHub Desktop.
properly set search_path in cluster, database, role, session, function level.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--https://stackoverflow.com/questions/9067335/how-does-the-search-path-influence-identifier-resolution-and-the-current-schema/9067777#9067777 | |
--properly set search path. | |
--Set it in cluster level. | |
ALTER SYSTEM SET search_path TO 'public'; | |
SELECT pg_reload_conf(); | |
--Set it as default for database test16. | |
ALTER DATABASE test16 SET search_path = '$user', 'public'; | |
-- Set it as default for the role (bob) you connect with (effective cluster-wide): | |
ALTER ROLE bob SET search_path = '$user', 'public'; | |
--Set as default for a role(bob) in a database (test16). | |
ALTER ROLE bob IN DATABASE test16 SET search_path = '$user', 'public'; | |
--Set it in a session level. | |
SET search_path = '$user', 'public'; | |
--set it for the function. | |
CREATE OR REPLACE FUNCTION test_search_path () | |
RETURNS void | |
LANGUAGE plpgsql | |
SECURITY DEFINER | |
SET search_path = new, public, pg_temp | |
AS $fun$ | |
BEGIN | |
--query part | |
END | |
$fun$; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment