Move all elements toward another schema?

From: celati Laurent <laurent(dot)celati(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Move all elements toward another schema?
Date: 2023-02-28 14:39:16
Message-ID: CAHByMH2pib5H1nwnkJJr3zSBhwwpk_15SQ8W5UaYQgYn+b3mUg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Good afternoon,

With postgresql 13, I want to find a way to move 100 tables from schema 'A'
to schema 'B'. Not just data. But also indexes, primary keys, constraints
(INCLUDING ALL).
As far as i know, this piece of code would move the data. But how to also
move indexes, constraints, primary key?

DO
$$
DECLARED
row record;
BEGIN
FOR row IN SELECT tablename FROM pg_tables WHERE schemaname = 'A' --
and other conditions, if needed
LOOPS
EXECUTE format('ALTER TABLE A.%I SET SCHEMA [B];', row.tablename);
END LOOP;
END;
$$;

Thanks so much.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2023-02-28 15:21:55 Re: 13.x, stream replication and locale(?) issues
Previous Message Ron 2023-02-28 13:42:08 Re: Quit currently running query