Creating all tables for a foreign schema

This recipe provides an example of creating a single foreign table, although in an actual production system, this process could be quite cumbersome. Do we really want to create dozens or even hundreds of tables one by one? In PostgreSQL 9.5 and more, we can actually import the entire foreign schema.

The test data we're using is the default set of tables created by the pgbench tool. This means all of the tables exist in the public schema. With this knowledge, we could substitute this command for the CREATE FOREIGN TABLE step in our recipe:

IMPORT FOREIGN SCHEMA public
  FROM SERVER primary_db
  INTO public;

Of course, importing the public schema is not a recommended practice. Yet it's clear we can utilize ...

Get PostgreSQL High Availability Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.