PostgreSQL – Difference Between ALTER DATABASE SET search_path and SET search_path

postgresql

PostgreSQL 9.6. I just found both of these commands in a flyway migration, right next to each other. I feel like they are probably redundant but how do these statements differ? Is there a reason to use both? Apologies if this is a duplicate; I couldn't find an answer.

Best Answer

ALTER DATABASE will change the search_path for every new connection to that database. The change however does not affect current connections - including the one that ran the ALTER DATABASE. The set search_path then changes the path also for the current connection so that it has the same value as the default path.