PostgreSQL – How to Create Full Copy of Database Schema in Same Database

postgresql

How I can copy my public schema into the same database with full table structure, data, functions, fk, pk and etc.
My version of Postgres is 8.4
P.S. I need to copy schema NOT database

Best Answer

There's no simple way to do this in pg_dump/pg_restore itself. You could try the following if you are able to remove the database temporarily.

  1. Take a dump of your public schema using pg_dump
  2. run "ALTER SCHEMA public RENAME TO public_copy"
  3. Restore your dump of your public schema from step 1 using pg_restore