how to automatically create the associated schema when I `pg_restore` a table?












0















I backed a table into a file using something like pg_dump --table=ex.tab.



But when I restore the dump file into a newly created database, it complains that the schema ex does not exist. Looking into the dump file ( listed below), it does not have a CREATE SCHEMA statement.



Is there an option that I can add to pg_dump or pg_store so that the schema of the table is automatically created?



Note: I do this for multiple tables in the command line (multiple --table=), and do not want to manually create schema for each table beforehand (if possible).



--
-- PostgreSQL database dump
--

-- Dumped from database version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)
-- Dumped by pg_dump version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: tab; Type: TABLE; Schema: ex; Owner: myuser
--

CREATE TABLE ex.tab (
name character varying(80),
state_ character varying(2)
);


ALTER TABLE ex.tab OWNER TO myuser;

--
-- Data for Name: tab; Type: TABLE DATA; Schema: ex; Owner: myuser
--

COPY ex.tab (name, state_) FROM stdin;
San Francisco CA
ST LOUIS MO
LOS ANGELES CA
.


--
-- PostgreSQL database dump complete
--








share



























    0















    I backed a table into a file using something like pg_dump --table=ex.tab.



    But when I restore the dump file into a newly created database, it complains that the schema ex does not exist. Looking into the dump file ( listed below), it does not have a CREATE SCHEMA statement.



    Is there an option that I can add to pg_dump or pg_store so that the schema of the table is automatically created?



    Note: I do this for multiple tables in the command line (multiple --table=), and do not want to manually create schema for each table beforehand (if possible).



    --
    -- PostgreSQL database dump
    --

    -- Dumped from database version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)
    -- Dumped by pg_dump version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)

    SET statement_timeout = 0;
    SET lock_timeout = 0;
    SET idle_in_transaction_session_timeout = 0;
    SET client_encoding = 'UTF8';
    SET standard_conforming_strings = on;
    SELECT pg_catalog.set_config('search_path', '', false);
    SET check_function_bodies = false;
    SET client_min_messages = warning;
    SET row_security = off;

    SET default_tablespace = '';

    SET default_with_oids = false;

    --
    -- Name: tab; Type: TABLE; Schema: ex; Owner: myuser
    --

    CREATE TABLE ex.tab (
    name character varying(80),
    state_ character varying(2)
    );


    ALTER TABLE ex.tab OWNER TO myuser;

    --
    -- Data for Name: tab; Type: TABLE DATA; Schema: ex; Owner: myuser
    --

    COPY ex.tab (name, state_) FROM stdin;
    San Francisco CA
    ST LOUIS MO
    LOS ANGELES CA
    .


    --
    -- PostgreSQL database dump complete
    --








    share

























      0












      0








      0








      I backed a table into a file using something like pg_dump --table=ex.tab.



      But when I restore the dump file into a newly created database, it complains that the schema ex does not exist. Looking into the dump file ( listed below), it does not have a CREATE SCHEMA statement.



      Is there an option that I can add to pg_dump or pg_store so that the schema of the table is automatically created?



      Note: I do this for multiple tables in the command line (multiple --table=), and do not want to manually create schema for each table beforehand (if possible).



      --
      -- PostgreSQL database dump
      --

      -- Dumped from database version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)
      -- Dumped by pg_dump version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)

      SET statement_timeout = 0;
      SET lock_timeout = 0;
      SET idle_in_transaction_session_timeout = 0;
      SET client_encoding = 'UTF8';
      SET standard_conforming_strings = on;
      SELECT pg_catalog.set_config('search_path', '', false);
      SET check_function_bodies = false;
      SET client_min_messages = warning;
      SET row_security = off;

      SET default_tablespace = '';

      SET default_with_oids = false;

      --
      -- Name: tab; Type: TABLE; Schema: ex; Owner: myuser
      --

      CREATE TABLE ex.tab (
      name character varying(80),
      state_ character varying(2)
      );


      ALTER TABLE ex.tab OWNER TO myuser;

      --
      -- Data for Name: tab; Type: TABLE DATA; Schema: ex; Owner: myuser
      --

      COPY ex.tab (name, state_) FROM stdin;
      San Francisco CA
      ST LOUIS MO
      LOS ANGELES CA
      .


      --
      -- PostgreSQL database dump complete
      --








      share














      I backed a table into a file using something like pg_dump --table=ex.tab.



      But when I restore the dump file into a newly created database, it complains that the schema ex does not exist. Looking into the dump file ( listed below), it does not have a CREATE SCHEMA statement.



      Is there an option that I can add to pg_dump or pg_store so that the schema of the table is automatically created?



      Note: I do this for multiple tables in the command line (multiple --table=), and do not want to manually create schema for each table beforehand (if possible).



      --
      -- PostgreSQL database dump
      --

      -- Dumped from database version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)
      -- Dumped by pg_dump version 11.1 (Ubuntu 11.1-3.pgdg18.04+1)

      SET statement_timeout = 0;
      SET lock_timeout = 0;
      SET idle_in_transaction_session_timeout = 0;
      SET client_encoding = 'UTF8';
      SET standard_conforming_strings = on;
      SELECT pg_catalog.set_config('search_path', '', false);
      SET check_function_bodies = false;
      SET client_min_messages = warning;
      SET row_security = off;

      SET default_tablespace = '';

      SET default_with_oids = false;

      --
      -- Name: tab; Type: TABLE; Schema: ex; Owner: myuser
      --

      CREATE TABLE ex.tab (
      name character varying(80),
      state_ character varying(2)
      );


      ALTER TABLE ex.tab OWNER TO myuser;

      --
      -- Data for Name: tab; Type: TABLE DATA; Schema: ex; Owner: myuser
      --

      COPY ex.tab (name, state_) FROM stdin;
      San Francisco CA
      ST LOUIS MO
      LOS ANGELES CA
      .


      --
      -- PostgreSQL database dump complete
      --






      postgresql pg-dump pg-restore





      share












      share










      share



      share










      asked 1 min ago









      tinlyxtinlyx

      1,24431130




      1,24431130






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228649%2fhow-to-automatically-create-the-associated-schema-when-i-pg-restore-a-table%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f228649%2fhow-to-automatically-create-the-associated-schema-when-i-pg-restore-a-table%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL Server 17 - Attemping to backup to remote NAS but Access is denied

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Restoring from pg_dump with foreign key constraints