How to duplicate schemas in PostgreSQL

前端 未结 5 1754
日久生厌
日久生厌 2021-02-01 04:09

I have a database with schema public and schema_A. I need to create a new schema schema_b with the same structure than schema_a

5条回答
  •  忘掉有多难
    2021-02-01 04:16

    Just ran into same. Sometimes I am missing remap_schema :)
    The problem - neither from above addresses the Fc - standard format which is crucial for large schemas.
    So I came up with something which uses it :
    Pseudo code below - should work.
    Requires rename of source for duration of pg_dump which, of course, might not be an option :(
    Source :

    pg_dump --pre-data in sql format
    psql rename sosurce to target
    pg_dump -Fc --data-only
    psql rename back
    pg_dump --post-data in sql format
    

    Target :

    sed source_schema->target_schema pre-data sql |psql
    pg_restore Fc dump
    sed source_schema->target_schema post-data sql |psql
    

    sed above usually will include any other manipulations ( say different user names between source and target ) But it will be way much faster as data will not be part of the file

提交回复
热议问题