Export "public" from db2 (grant grant and ownership):
pg_dump -xO -n public db2 > db2.sql
The exported file will set the search path (somewhere near the top):
SET search_path = public, pg_catalog;
change it to:
CREATE SCHEMA IF NOT EXISTS new_schema; SET search_path = new_schema, pg_catalog;
Import to db1 as usual:
psql db1 < db2.sql
You probably want to transfer everything from the public to the new schema in db1 first.
If the circuit is already configured in db1, you can do the transfer in one go:
pg_dump -xO -n public db2 | sed 's/search_path = public/search_path = new_schema/' | psql db1
I would not recommend this without a lot of tests, of course.
source share