Search code examples
postgresqlschemadatabase-tablesearch-path

How does the search_path influence identifier resolution and the "current schema"


Is it possible to define in which schema new tables get created by default? (Referred by "unqualified table names".)

I've seen some details about using the "search path" in Postgres, but I think it only works while retrieving data, not creating.

I have a bunch of SQL scripts, which create many tables. Instead of modifying the scripts, I want to set the database create tables in a specific schema by default - when they have unqualified names.

Is this possible?


Solution

  • Search path is indeed what you want:

    % create schema blarg;
    % set search_path to blarg;
    % create table foo (id int);
    % \d
           List of relations
     Schema | Name | Type  | Owner 
    --------+------+-------+-------
     blarg  | foo  | table | pgsql