How to duplicate schemas in PostgreSQL

前端 未结 5 1636
日久生厌
日久生厌 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:25

    I would use pg_dump to dump the schema without data:

    -s
    --schema-only
    

    Dump only the object definitions (schema), not data.

    This option is the inverse of --data-only. It is similar to, but for historical reasons not identical to, specifying --section=pre-data --section=post-data.

    (Do not confuse this with the --schema option, which uses the word "schema" in a different meaning.)

    To exclude table data for only a subset of tables in the database, see --exclude-table-data.

    pg_dump $DB -p $PORT -n $SCHEMA -s -f filename.pgsql
    

    Then rename the schema in the dump (search & replace) and restore it with psql.

    psql $DB -f filename.pgsql
    

    Foreign key constraints referencing tables in other schemas are copied to point to the same schema.
    References to tables within the same schema point to the respective tables within the copied schema.

提交回复
热议问题