0

I have customer table in public schema. And I have a schema named after each customer. Each customer schema contains different tables like portals, plugin, ... When I want to select portals used by customer 'a', I can do select * from a.portal.

I want to select all the customers having a particular 'portal name'. But portal table is inside customer schema. So how should I query in this situation?

I have found solution using a PHP script but is there anything that we can do on the DB level?

Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
asked Feb 23, 2015 at 1:51
2
  • 1
    In order to get a meaningful answer (longer than 'Look into dynamic SQL'), please give an example of how portal tables differ among customers. Commented Feb 23, 2015 at 9:18
  • actually description of each portal inside every customer schema (for example a.portal or b.portal,where a and b are customers )is same.they don't differ in structure.thanks in advance. Commented Feb 24, 2015 at 4:05

1 Answer 1

1

You could use inheritance for this. You would have a "master" schema with template tables and only the basic set of columns that all inheriting tables share:

CREATE TABLE master.portal(portal_name text);

All other tables in various schemata inherit from it, while possibly adding more local columns:

CREATE TABLE a.portal(portal_id serial PRIMARY KEY, col2 text)
INHERITS (master.portal);
CREATE TABLE b.portal(portal_id serial PRIMARY KEY, col2 text, col3 text)
INHERITS (master.portal);

A SELECT query targeted on the master table would then search all child tables as well:

SELECT portal_name, tableoid::regclass::text AS source
FROM master.portal
WHERE portal_name ILIKE '%test%';

More details in this closely related answer on SO:

answered Feb 25, 2015 at 9:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.