Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from 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. – GoatWalker Jan 10 '12 at 14:41
up vote 11 down vote accepted

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
share|improve this answer
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 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
Simply renaming the schema will not updated references inside functions: 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

share|improve this answer
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

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.

share|improve this answer
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

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 backup file a name 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.

5.Click the Schemas root and right click it in the object browser and click create new schema. This will be the schema you are coping into.

5.Right click the new schema from step 5 and click restore. Restore from backup in step 3.

6.Rename new schema to new name.

7.Rename schema name change from step 4 back to original name.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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