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
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.