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

migrated from stackoverflow.com Jan 10 '12 at 21:32

This question came from our site for professional and enthusiast programmers.

  • Do you have more schemas, or only public? – a_horse_with_no_name Jan 10 '12 at 13:35
  • I have public and demo schemas. And I need to copy demo for creating demo account... – sigra Jan 10 '12 at 13:45
  • This would be a handy thing to be able to do. – Kuberchaun Jan 10 '12 at 14:41

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
  • how I can access to pg_dump and pg_restore via PHP? – sigra Jan 10 '12 at 13:24
  • That depends on where you're running the database. You can access pg_dump and pg_restore from the shell on your server. If you don't have shell access then you could try using PHPs shell_exec, otherwise you'll need to look into an alternative backup method, perhaps using a PostgreSQL GUI Tool – user1113185 Jan 10 '12 at 14:01
  • 1
    +1 That is the smartest solution so far. Shell command would look something like this (more in the the manual): pg_dump -n my_schema -f '/path/to/file.pgsql' my_db. Easiest as superuser (postgres) with pw-less peer authorization in pg_haba.conf. Restore after you have renamed the original schema: psql my_db -f '/path/to/file.pgsql'. If you have a plain SQL dump, you do not need pg_restore. – Erwin Brandstetter Jan 10 '12 at 22:29
  • There is an easy way, see my answer. pg_dump supports the -n switch for choosing a schema. Then just edit the schema name in the dump and reload. – Scott Marlowe Jan 11 '12 at 3:01
  • 2
    Simply renaming the schema will not updated references inside functions: gist.github.com/pschultz/5387172. Replacing the name is the dump is much more reliable if you get your search and replace right. – Peter Apr 15 '13 at 10:24
pg_dump -n schema_name > dump.sql
vi dump.sql # edit the schema name
psql: psql -f dump.sql

If you're stuck with php then use either back tics

`/usr/bin/pg_dump-n myschema mydb -U username > /tmp/dump.sql`

or the exec() command. For the change you can use sed the same way.

Here are 6 more chars

  • 1
    it is safer to rename the schema and load back the backed up original schema, especially when schema name might appear as contents (e.g. public). – artm Jun 29 '15 at 14:19

Using pgAdmin you can do the following. It's pretty manual, but might be all you need. A script based approach would be much more desirable. Not sure how well this will work if you don't have admin access and if your database is large, but should work just fine on a development database that you just have on your local computer.

  1. Right-click schema name you want to copy and click Backup. (You can go deeper than this and choose to just backup the structure instead of both).

  2. Give the backup file a name and also choose a format. (I usually use Tar.)

  3. Click Backup.

  4. Right-click the schema you backed up from and click properties and rename it to something else temporarily. (e.g. temprename)

  5. Click the schemas root and right-click it in the object browser and click create new schema and give the schema the name public. This will be the schema you are copying into from your backup.

  6. Right-click the new schema public from step 5. and click restore. Restore from the backup file in step 3.

  7. Rename new schema public to a different name (e.g. newschema).

  8. Rename schema temprename change from step 4 back to the original name.

  • The new schema created in step 5 must have the same name as the schema you backed up, otherwise pgAdmin won't restore anything. – Cao Minh Tu Mar 2 '13 at 1:46

You could use


Then drop all schemas you don't need:


The only drawback is all connections to old_db must be determinated before you can create the copy (so the process that runs the CREATE DATABASE statement must connect e.g. to template1)

If that is not an option, pg_dump/pg_restore is the only way to do it.

  • 1
    Before I asked this question, I used a similar method of cloning a database. But it spends a lot of time and I think that cloning of only schema is much faster... – sigra Jan 10 '12 at 17:07
  • @sigra: a_horse's method of cloning is the fastest available for databases, because actual files can be just copied, which saves a lot of overhead. I doubt a dump and reload of the schema will be faster unless the schema is only a small part of the whole db. So, +1 for this answer even if it does not answer the actual question asked. – Erwin Brandstetter Jan 11 '12 at 1:34
  • That's a lot of work to clone a single schema. dumping the schema, renaming it in the dump and reloading is much faster. – Scott Marlowe Jan 11 '12 at 4:46
  • @ScottMarlowe: depends on which the largest schema is. If the largest is one of the dropped ones, then yes I agree. – a_horse_with_no_name Jan 11 '12 at 8:02

expanding on user1113185 answer, here's a full workflow using psql/pg_dump.

The following exports all objects of old_schema and imports them into new new_schema schema, as user, in dbname database:

psql -U user -d dbname -c 'ALTER SCHEMA old_schema RENAME TO new_schema'
pg_dump -U user -n new_schema -f new_schema.sql dbname
psql -U user -d dbname -c 'ALTER SCHEMA new_schema RENAME TO old_schema'
psql -U user -d dbname -c 'CREATE SCHEMA new_schema'
psql -U user -q -d dbname -f new_schema.sql
rm new_schema.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.