I'm using pg_dump (9.2 database), to extract one schema and load this into another database (9.4).

well, here's the command:

pg_dump -f file.sql -F plain --inserts -n schema database

Ok. My command finished up really nice, but, it's not dumping everything to a schema called schema (in the example above). It's loading everything into public.


To make the question clearer:

The dump works fine. Upon inspection, it's dumping my schema.table, for example, to public.table. I want that to restored to schema, not public.

Any tips on solving this?


  • What permissions does the user, that you create the dump with, have on the schema? Commented May 4, 2015 at 22:22
  • I suspect you've misunderstood what -n does. It should be restricting the dump to only objects in the schema schema, so objects in public and other schemas are not dumped. Commented May 5, 2015 at 5:02
  • 1
    @CraigRinger: It does just that. It restricted the dump to schema. What I maybe could not explain, is that I expect the schema to restored in it's original place. Everything is just going to public, instead. Commented May 5, 2015 at 13:17

1 Answer 1


When dumping a schema with the mentioned command, as postgres user:

pg_dump -f file.sql -F plain --inserts -n schemaname database

it produces a file whose basic structure looks like:

-- various SET commands
CREATE SCHEMA schemaname;

ALTER SCHEMA schemaname OWNER TO ownername;

SET search_path = schemaname, pg_catalog;

CREATE TABLE tablename(...) ...

ALTER TABLE schemaname.tablename OWNER TO ownername;

COPY tablename (columns...) FROM stdin;

The fact that search_path is set to the newly created schema implies that the CREATE TABLE and COPY statements that follow will happen in that schema and not in the public schema.

Or SET search_path would have to fail, which shouldn't happen even if the creation of the schema just above in the script failed. If schemaname does not exist, the next schema tried would be pg_catalog and CREATE TABLE would refuse to write into it, and after that there's no next schema in the search path, and in particular not public.

It's hard to imagine what mishappening could cause the above script to end up creating and populating anything in the public schema.

  • In non--n contexts (e.g., -t - single-table), the SET search_path = schemaname, pg_catalog; is something I really cannot fathom. It basically guarantees that if the schema doesn't exist in the target DB, all the table creation logic will fail, so why not just test for the existence of the schema in the target DB, and fail (or create schema) if it doesn't exist? Sometimes folks want to replicate a table to a different schema - in my work I reckon it's 75% of the use-cases, and it's a pain having to FART-edit the schema-stuff (it's the only thing I use FART for instead of awk/grep).
    – GT.
    Commented Jul 15, 2016 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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