Select (retrieve) all records from multiple schemas using Postgres

后端 未结 2 1398
囚心锁ツ
囚心锁ツ 2020-11-30 13:27

I have a PostgreSQL database with some schemas, like below:

My_Database
 |-> Schemas
    |-> AccountA
    |-> AccountB
    |-> AccountC
    |->         


        
2条回答
  •  一整个雨季
    2020-11-30 13:50

    You basically want a union all:

    SELECT title FROM AccountA.product WHERE title ILIKE '%test%'
    UNION ALL
    SELECT title FROM AccountB.product WHERE title ILIKE '%test%'
    UNION ALL
    ...;
    

    You can do so automatically by using dynamic SQL and the catalog to locate all AccountXYZ schemas that have a products table.

    Alternatively, create a AllAccounts schema with similar tables as the ones in individual schemas, and use table inheritance.

    Note that neither will tell you which schema the data is from, however. In the former case, this is easy enough to add; not so much in the latter unless you add an extra column.

提交回复
热议问题