How can I export the schema of a database in PostgreSQL?












69














My computer broke down but fortunately I backed up
the folder C:Program FilesPostgreSQL.



Now I'm working in a new computer and I would like to import the previous
Postgres databases that are stored in the external disk.



I would like to export the schema of a specific database
that is located in the backup folder.



The file PostgreSQL8.3dataglobalpg_database contains information
about databases and their OIDs; for example:



"db1" 20012
"db2" 23456


I would like to export the schema of "db1".



There is a folder named "20012" in folder "PostgreSQL8.3database20012"
that contains a lot of files [500 files].



Is there any way to export the schema of that database?



Note that all of the Postgresql database files are located
in an external hard disk and I would like to export the schema of that database in an SQL file, take that file, run it and create the same exact database locally.










share|improve this question





























    69














    My computer broke down but fortunately I backed up
    the folder C:Program FilesPostgreSQL.



    Now I'm working in a new computer and I would like to import the previous
    Postgres databases that are stored in the external disk.



    I would like to export the schema of a specific database
    that is located in the backup folder.



    The file PostgreSQL8.3dataglobalpg_database contains information
    about databases and their OIDs; for example:



    "db1" 20012
    "db2" 23456


    I would like to export the schema of "db1".



    There is a folder named "20012" in folder "PostgreSQL8.3database20012"
    that contains a lot of files [500 files].



    Is there any way to export the schema of that database?



    Note that all of the Postgresql database files are located
    in an external hard disk and I would like to export the schema of that database in an SQL file, take that file, run it and create the same exact database locally.










    share|improve this question



























      69












      69








      69


      12





      My computer broke down but fortunately I backed up
      the folder C:Program FilesPostgreSQL.



      Now I'm working in a new computer and I would like to import the previous
      Postgres databases that are stored in the external disk.



      I would like to export the schema of a specific database
      that is located in the backup folder.



      The file PostgreSQL8.3dataglobalpg_database contains information
      about databases and their OIDs; for example:



      "db1" 20012
      "db2" 23456


      I would like to export the schema of "db1".



      There is a folder named "20012" in folder "PostgreSQL8.3database20012"
      that contains a lot of files [500 files].



      Is there any way to export the schema of that database?



      Note that all of the Postgresql database files are located
      in an external hard disk and I would like to export the schema of that database in an SQL file, take that file, run it and create the same exact database locally.










      share|improve this question















      My computer broke down but fortunately I backed up
      the folder C:Program FilesPostgreSQL.



      Now I'm working in a new computer and I would like to import the previous
      Postgres databases that are stored in the external disk.



      I would like to export the schema of a specific database
      that is located in the backup folder.



      The file PostgreSQL8.3dataglobalpg_database contains information
      about databases and their OIDs; for example:



      "db1" 20012
      "db2" 23456


      I would like to export the schema of "db1".



      There is a folder named "20012" in folder "PostgreSQL8.3database20012"
      that contains a lot of files [500 files].



      Is there any way to export the schema of that database?



      Note that all of the Postgresql database files are located
      in an external hard disk and I would like to export the schema of that database in an SQL file, take that file, run it and create the same exact database locally.







      postgresql schema export






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 4 '17 at 16:34









      Mark

      3,75931331




      3,75931331










      asked Jan 23 '13 at 17:57









      programmer

      1,305113552




      1,305113552
























          7 Answers
          7






          active

          oldest

          votes


















          100














          You should take a look at pg_dump:



          pg_dump -s databasename


          Will dump only the schema to stdout as .sql.



          For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.






          share|improve this answer





















          • are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
            – programmer
            Jan 23 '13 at 18:16










          • Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
            – Anew
            Jan 23 '13 at 18:24






          • 1




            no problem, thanks
            – programmer
            Jan 23 '13 at 18:33










          • i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
            – programmer
            Jan 23 '13 at 18:49






          • 7




            @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
            – Arivarasan L
            Feb 23 '15 at 13:23



















          26














          In Linux you can do like this



          pg_dump -U postgres -s postgres > exportFile.dmp


          Maybe it can work in Windows too,
          if not try the same with pg_dump.exe



          pg_dump.exe -U postgres -s postgres > exportFile.dmp





          share|improve this answer





















          • works the same way in Windows (and mac) FYI.
            – M T Head
            May 24 '17 at 1:26





















          17














          I am running Postgres 9.6 where I had to export a particular schema along with data.



          I used the following command:



          pg_dump.exe -U username -d databasename -n schemaname > C:mylocationmydumpfilename.dmp


          If you want only the schema without data, use the switch s instead of n



          Below is the pg_dump switch list:



          C:Program FilesPostgreSQL9.6bin>pg_dump --help
          pg_dump dumps a database as a text file or to other formats.

          Usage:
          pg_dump [OPTION]... [DBNAME]

          General options:
          -f, --file=FILENAME output file or directory name
          -F, --format=c|d|t|p output file format (custom, directory, tar,
          plain text (default))
          -j, --jobs=NUM use this many parallel jobs to dump
          -v, --verbose verbose mode
          -V, --version output version information, then exit
          -Z, --compress=0-9 compression level for compressed formats
          --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
          -?, --help show this help, then exit

          Options controlling the output content:
          -a, --data-only dump only the data, not the schema
          -b, --blobs include large objects in dump
          -c, --clean clean (drop) database objects before recreating
          -C, --create include commands to create database in dump
          -E, --encoding=ENCODING dump the data in encoding ENCODING
          -n, --schema=SCHEMA dump the named schema(s) only
          -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
          -o, --oids include OIDs in dump
          -O, --no-owner skip restoration of object ownership in
          plain-text format
          -s, --schema-only dump only the schema, no data
          -S, --superuser=NAME superuser user name to use in plain-text format
          -t, --table=TABLE dump the named table(s) only
          -T, --exclude-table=TABLE do NOT dump the named table(s)
          -x, --no-privileges do not dump privileges (grant/revoke)
          --binary-upgrade for use by upgrade utilities only
          --column-inserts dump data as INSERT commands with column names
          --disable-dollar-quoting disable dollar quoting, use SQL standard quoting
          --disable-triggers disable triggers during data-only restore
          --enable-row-security enable row security (dump only content user has
          access to)
          --exclude-table-data=TABLE do NOT dump data for the named table(s)
          --if-exists use IF EXISTS when dropping objects
          --inserts dump data as INSERT commands, rather than COPY
          --no-security-labels do not dump security label assignments
          --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
          --no-tablespaces do not dump tablespace assignments
          --no-unlogged-table-data do not dump unlogged table data
          --quote-all-identifiers quote all identifiers, even if not key words
          --section=SECTION dump named section (pre-data, data, or post-data)
          --serializable-deferrable wait until the dump can run without anomalies
          --snapshot=SNAPSHOT use given snapshot for the dump
          --strict-names require table and/or schema include patterns to
          match at least one entity each
          --use-set-session-authorization
          use SET SESSION AUTHORIZATION commands instead of
          ALTER OWNER commands to set ownership

          Connection options:
          -d, --dbname=DBNAME database to dump
          -h, --host=HOSTNAME database server host or socket directory
          -p, --port=PORT database server port number
          -U, --username=NAME connect as specified database user
          -w, --no-password never prompt for password
          -W, --password force password prompt (should happen automatically)
          --role=ROLENAME do SET ROLE before dump

          If no database name is supplied, then the PGDATABASE environment
          variable value is used.

          Report bugs to <pgsql-bugs@postgresql.org>.





          share|improve this answer





























            4














            If you only want the create tables, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'






            share|improve this answer





























              2














              set up a new postgresql server and replace its data folder with the files from your external disk.



              You will then be able to start that postgresql server up and retrieve the data using pg_dump (pg_dump -s for the schema-only as mentioned)






              share|improve this answer





















              • i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                – programmer
                Jan 23 '13 at 18:44












              • are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                – drone.ah
                Jan 23 '13 at 18:54










              • well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                – programmer
                Jan 23 '13 at 18:59












              • Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                – programmer
                Jan 23 '13 at 19:06








              • 1




                it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                – drone.ah
                Jan 23 '13 at 19:13



















              0














              You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here






              share|improve this answer





























                0














                pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>


                Please notice that you have sufficient privilege to access that schema.
                If you want take backup as specific user add user name in that command preceded by -U






                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  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: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  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%2fstackoverflow.com%2fquestions%2f14486241%2fhow-can-i-export-the-schema-of-a-database-in-postgresql%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  100














                  You should take a look at pg_dump:



                  pg_dump -s databasename


                  Will dump only the schema to stdout as .sql.



                  For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.






                  share|improve this answer





















                  • are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                    – programmer
                    Jan 23 '13 at 18:16










                  • Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                    – Anew
                    Jan 23 '13 at 18:24






                  • 1




                    no problem, thanks
                    – programmer
                    Jan 23 '13 at 18:33










                  • i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                    – programmer
                    Jan 23 '13 at 18:49






                  • 7




                    @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                    – Arivarasan L
                    Feb 23 '15 at 13:23
















                  100














                  You should take a look at pg_dump:



                  pg_dump -s databasename


                  Will dump only the schema to stdout as .sql.



                  For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.






                  share|improve this answer





















                  • are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                    – programmer
                    Jan 23 '13 at 18:16










                  • Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                    – Anew
                    Jan 23 '13 at 18:24






                  • 1




                    no problem, thanks
                    – programmer
                    Jan 23 '13 at 18:33










                  • i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                    – programmer
                    Jan 23 '13 at 18:49






                  • 7




                    @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                    – Arivarasan L
                    Feb 23 '15 at 13:23














                  100












                  100








                  100






                  You should take a look at pg_dump:



                  pg_dump -s databasename


                  Will dump only the schema to stdout as .sql.



                  For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.






                  share|improve this answer












                  You should take a look at pg_dump:



                  pg_dump -s databasename


                  Will dump only the schema to stdout as .sql.



                  For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 23 '13 at 18:13









                  Anew

                  3,74111834




                  3,74111834












                  • are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                    – programmer
                    Jan 23 '13 at 18:16










                  • Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                    – Anew
                    Jan 23 '13 at 18:24






                  • 1




                    no problem, thanks
                    – programmer
                    Jan 23 '13 at 18:33










                  • i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                    – programmer
                    Jan 23 '13 at 18:49






                  • 7




                    @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                    – Arivarasan L
                    Feb 23 '15 at 13:23


















                  • are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                    – programmer
                    Jan 23 '13 at 18:16










                  • Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                    – Anew
                    Jan 23 '13 at 18:24






                  • 1




                    no problem, thanks
                    – programmer
                    Jan 23 '13 at 18:33










                  • i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                    – programmer
                    Jan 23 '13 at 18:49






                  • 7




                    @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                    – Arivarasan L
                    Feb 23 '15 at 13:23
















                  are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                  – programmer
                  Jan 23 '13 at 18:16




                  are you sure that pg_dump is going to work? Cause the files of the database are located in an external disk and not locally...
                  – programmer
                  Jan 23 '13 at 18:16












                  Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                  – Anew
                  Jan 23 '13 at 18:24




                  Provided you can start the old database server up, yes. pg_dump can connect remotely to a database server as you probably know. I'd attempt to install 8.3 on your new machine and copy over your old database files and start postgres up and see if that works. I thought you were asking more generally about dumping the schema only, my bad.
                  – Anew
                  Jan 23 '13 at 18:24




                  1




                  1




                  no problem, thanks
                  – programmer
                  Jan 23 '13 at 18:33




                  no problem, thanks
                  – programmer
                  Jan 23 '13 at 18:33












                  i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                  – programmer
                  Jan 23 '13 at 18:49




                  i have already installed the 8.3 version of postgresql, copied the data folder to the new installation, but i can't see my old databases...
                  – programmer
                  Jan 23 '13 at 18:49




                  7




                  7




                  @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                  – Arivarasan L
                  Feb 23 '15 at 13:23




                  @Anew thanks it works. pg_dump -s databasename > schema.sql will outputs the result into schema.sql file
                  – Arivarasan L
                  Feb 23 '15 at 13:23













                  26














                  In Linux you can do like this



                  pg_dump -U postgres -s postgres > exportFile.dmp


                  Maybe it can work in Windows too,
                  if not try the same with pg_dump.exe



                  pg_dump.exe -U postgres -s postgres > exportFile.dmp





                  share|improve this answer





















                  • works the same way in Windows (and mac) FYI.
                    – M T Head
                    May 24 '17 at 1:26


















                  26














                  In Linux you can do like this



                  pg_dump -U postgres -s postgres > exportFile.dmp


                  Maybe it can work in Windows too,
                  if not try the same with pg_dump.exe



                  pg_dump.exe -U postgres -s postgres > exportFile.dmp





                  share|improve this answer





















                  • works the same way in Windows (and mac) FYI.
                    – M T Head
                    May 24 '17 at 1:26
















                  26












                  26








                  26






                  In Linux you can do like this



                  pg_dump -U postgres -s postgres > exportFile.dmp


                  Maybe it can work in Windows too,
                  if not try the same with pg_dump.exe



                  pg_dump.exe -U postgres -s postgres > exportFile.dmp





                  share|improve this answer












                  In Linux you can do like this



                  pg_dump -U postgres -s postgres > exportFile.dmp


                  Maybe it can work in Windows too,
                  if not try the same with pg_dump.exe



                  pg_dump.exe -U postgres -s postgres > exportFile.dmp






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 24 '15 at 5:41









                  lev09

                  487410




                  487410












                  • works the same way in Windows (and mac) FYI.
                    – M T Head
                    May 24 '17 at 1:26




















                  • works the same way in Windows (and mac) FYI.
                    – M T Head
                    May 24 '17 at 1:26


















                  works the same way in Windows (and mac) FYI.
                  – M T Head
                  May 24 '17 at 1:26






                  works the same way in Windows (and mac) FYI.
                  – M T Head
                  May 24 '17 at 1:26













                  17














                  I am running Postgres 9.6 where I had to export a particular schema along with data.



                  I used the following command:



                  pg_dump.exe -U username -d databasename -n schemaname > C:mylocationmydumpfilename.dmp


                  If you want only the schema without data, use the switch s instead of n



                  Below is the pg_dump switch list:



                  C:Program FilesPostgreSQL9.6bin>pg_dump --help
                  pg_dump dumps a database as a text file or to other formats.

                  Usage:
                  pg_dump [OPTION]... [DBNAME]

                  General options:
                  -f, --file=FILENAME output file or directory name
                  -F, --format=c|d|t|p output file format (custom, directory, tar,
                  plain text (default))
                  -j, --jobs=NUM use this many parallel jobs to dump
                  -v, --verbose verbose mode
                  -V, --version output version information, then exit
                  -Z, --compress=0-9 compression level for compressed formats
                  --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
                  -?, --help show this help, then exit

                  Options controlling the output content:
                  -a, --data-only dump only the data, not the schema
                  -b, --blobs include large objects in dump
                  -c, --clean clean (drop) database objects before recreating
                  -C, --create include commands to create database in dump
                  -E, --encoding=ENCODING dump the data in encoding ENCODING
                  -n, --schema=SCHEMA dump the named schema(s) only
                  -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
                  -o, --oids include OIDs in dump
                  -O, --no-owner skip restoration of object ownership in
                  plain-text format
                  -s, --schema-only dump only the schema, no data
                  -S, --superuser=NAME superuser user name to use in plain-text format
                  -t, --table=TABLE dump the named table(s) only
                  -T, --exclude-table=TABLE do NOT dump the named table(s)
                  -x, --no-privileges do not dump privileges (grant/revoke)
                  --binary-upgrade for use by upgrade utilities only
                  --column-inserts dump data as INSERT commands with column names
                  --disable-dollar-quoting disable dollar quoting, use SQL standard quoting
                  --disable-triggers disable triggers during data-only restore
                  --enable-row-security enable row security (dump only content user has
                  access to)
                  --exclude-table-data=TABLE do NOT dump data for the named table(s)
                  --if-exists use IF EXISTS when dropping objects
                  --inserts dump data as INSERT commands, rather than COPY
                  --no-security-labels do not dump security label assignments
                  --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
                  --no-tablespaces do not dump tablespace assignments
                  --no-unlogged-table-data do not dump unlogged table data
                  --quote-all-identifiers quote all identifiers, even if not key words
                  --section=SECTION dump named section (pre-data, data, or post-data)
                  --serializable-deferrable wait until the dump can run without anomalies
                  --snapshot=SNAPSHOT use given snapshot for the dump
                  --strict-names require table and/or schema include patterns to
                  match at least one entity each
                  --use-set-session-authorization
                  use SET SESSION AUTHORIZATION commands instead of
                  ALTER OWNER commands to set ownership

                  Connection options:
                  -d, --dbname=DBNAME database to dump
                  -h, --host=HOSTNAME database server host or socket directory
                  -p, --port=PORT database server port number
                  -U, --username=NAME connect as specified database user
                  -w, --no-password never prompt for password
                  -W, --password force password prompt (should happen automatically)
                  --role=ROLENAME do SET ROLE before dump

                  If no database name is supplied, then the PGDATABASE environment
                  variable value is used.

                  Report bugs to <pgsql-bugs@postgresql.org>.





                  share|improve this answer


























                    17














                    I am running Postgres 9.6 where I had to export a particular schema along with data.



                    I used the following command:



                    pg_dump.exe -U username -d databasename -n schemaname > C:mylocationmydumpfilename.dmp


                    If you want only the schema without data, use the switch s instead of n



                    Below is the pg_dump switch list:



                    C:Program FilesPostgreSQL9.6bin>pg_dump --help
                    pg_dump dumps a database as a text file or to other formats.

                    Usage:
                    pg_dump [OPTION]... [DBNAME]

                    General options:
                    -f, --file=FILENAME output file or directory name
                    -F, --format=c|d|t|p output file format (custom, directory, tar,
                    plain text (default))
                    -j, --jobs=NUM use this many parallel jobs to dump
                    -v, --verbose verbose mode
                    -V, --version output version information, then exit
                    -Z, --compress=0-9 compression level for compressed formats
                    --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
                    -?, --help show this help, then exit

                    Options controlling the output content:
                    -a, --data-only dump only the data, not the schema
                    -b, --blobs include large objects in dump
                    -c, --clean clean (drop) database objects before recreating
                    -C, --create include commands to create database in dump
                    -E, --encoding=ENCODING dump the data in encoding ENCODING
                    -n, --schema=SCHEMA dump the named schema(s) only
                    -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
                    -o, --oids include OIDs in dump
                    -O, --no-owner skip restoration of object ownership in
                    plain-text format
                    -s, --schema-only dump only the schema, no data
                    -S, --superuser=NAME superuser user name to use in plain-text format
                    -t, --table=TABLE dump the named table(s) only
                    -T, --exclude-table=TABLE do NOT dump the named table(s)
                    -x, --no-privileges do not dump privileges (grant/revoke)
                    --binary-upgrade for use by upgrade utilities only
                    --column-inserts dump data as INSERT commands with column names
                    --disable-dollar-quoting disable dollar quoting, use SQL standard quoting
                    --disable-triggers disable triggers during data-only restore
                    --enable-row-security enable row security (dump only content user has
                    access to)
                    --exclude-table-data=TABLE do NOT dump data for the named table(s)
                    --if-exists use IF EXISTS when dropping objects
                    --inserts dump data as INSERT commands, rather than COPY
                    --no-security-labels do not dump security label assignments
                    --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
                    --no-tablespaces do not dump tablespace assignments
                    --no-unlogged-table-data do not dump unlogged table data
                    --quote-all-identifiers quote all identifiers, even if not key words
                    --section=SECTION dump named section (pre-data, data, or post-data)
                    --serializable-deferrable wait until the dump can run without anomalies
                    --snapshot=SNAPSHOT use given snapshot for the dump
                    --strict-names require table and/or schema include patterns to
                    match at least one entity each
                    --use-set-session-authorization
                    use SET SESSION AUTHORIZATION commands instead of
                    ALTER OWNER commands to set ownership

                    Connection options:
                    -d, --dbname=DBNAME database to dump
                    -h, --host=HOSTNAME database server host or socket directory
                    -p, --port=PORT database server port number
                    -U, --username=NAME connect as specified database user
                    -w, --no-password never prompt for password
                    -W, --password force password prompt (should happen automatically)
                    --role=ROLENAME do SET ROLE before dump

                    If no database name is supplied, then the PGDATABASE environment
                    variable value is used.

                    Report bugs to <pgsql-bugs@postgresql.org>.





                    share|improve this answer
























                      17












                      17








                      17






                      I am running Postgres 9.6 where I had to export a particular schema along with data.



                      I used the following command:



                      pg_dump.exe -U username -d databasename -n schemaname > C:mylocationmydumpfilename.dmp


                      If you want only the schema without data, use the switch s instead of n



                      Below is the pg_dump switch list:



                      C:Program FilesPostgreSQL9.6bin>pg_dump --help
                      pg_dump dumps a database as a text file or to other formats.

                      Usage:
                      pg_dump [OPTION]... [DBNAME]

                      General options:
                      -f, --file=FILENAME output file or directory name
                      -F, --format=c|d|t|p output file format (custom, directory, tar,
                      plain text (default))
                      -j, --jobs=NUM use this many parallel jobs to dump
                      -v, --verbose verbose mode
                      -V, --version output version information, then exit
                      -Z, --compress=0-9 compression level for compressed formats
                      --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
                      -?, --help show this help, then exit

                      Options controlling the output content:
                      -a, --data-only dump only the data, not the schema
                      -b, --blobs include large objects in dump
                      -c, --clean clean (drop) database objects before recreating
                      -C, --create include commands to create database in dump
                      -E, --encoding=ENCODING dump the data in encoding ENCODING
                      -n, --schema=SCHEMA dump the named schema(s) only
                      -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
                      -o, --oids include OIDs in dump
                      -O, --no-owner skip restoration of object ownership in
                      plain-text format
                      -s, --schema-only dump only the schema, no data
                      -S, --superuser=NAME superuser user name to use in plain-text format
                      -t, --table=TABLE dump the named table(s) only
                      -T, --exclude-table=TABLE do NOT dump the named table(s)
                      -x, --no-privileges do not dump privileges (grant/revoke)
                      --binary-upgrade for use by upgrade utilities only
                      --column-inserts dump data as INSERT commands with column names
                      --disable-dollar-quoting disable dollar quoting, use SQL standard quoting
                      --disable-triggers disable triggers during data-only restore
                      --enable-row-security enable row security (dump only content user has
                      access to)
                      --exclude-table-data=TABLE do NOT dump data for the named table(s)
                      --if-exists use IF EXISTS when dropping objects
                      --inserts dump data as INSERT commands, rather than COPY
                      --no-security-labels do not dump security label assignments
                      --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
                      --no-tablespaces do not dump tablespace assignments
                      --no-unlogged-table-data do not dump unlogged table data
                      --quote-all-identifiers quote all identifiers, even if not key words
                      --section=SECTION dump named section (pre-data, data, or post-data)
                      --serializable-deferrable wait until the dump can run without anomalies
                      --snapshot=SNAPSHOT use given snapshot for the dump
                      --strict-names require table and/or schema include patterns to
                      match at least one entity each
                      --use-set-session-authorization
                      use SET SESSION AUTHORIZATION commands instead of
                      ALTER OWNER commands to set ownership

                      Connection options:
                      -d, --dbname=DBNAME database to dump
                      -h, --host=HOSTNAME database server host or socket directory
                      -p, --port=PORT database server port number
                      -U, --username=NAME connect as specified database user
                      -w, --no-password never prompt for password
                      -W, --password force password prompt (should happen automatically)
                      --role=ROLENAME do SET ROLE before dump

                      If no database name is supplied, then the PGDATABASE environment
                      variable value is used.

                      Report bugs to <pgsql-bugs@postgresql.org>.





                      share|improve this answer












                      I am running Postgres 9.6 where I had to export a particular schema along with data.



                      I used the following command:



                      pg_dump.exe -U username -d databasename -n schemaname > C:mylocationmydumpfilename.dmp


                      If you want only the schema without data, use the switch s instead of n



                      Below is the pg_dump switch list:



                      C:Program FilesPostgreSQL9.6bin>pg_dump --help
                      pg_dump dumps a database as a text file or to other formats.

                      Usage:
                      pg_dump [OPTION]... [DBNAME]

                      General options:
                      -f, --file=FILENAME output file or directory name
                      -F, --format=c|d|t|p output file format (custom, directory, tar,
                      plain text (default))
                      -j, --jobs=NUM use this many parallel jobs to dump
                      -v, --verbose verbose mode
                      -V, --version output version information, then exit
                      -Z, --compress=0-9 compression level for compressed formats
                      --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
                      -?, --help show this help, then exit

                      Options controlling the output content:
                      -a, --data-only dump only the data, not the schema
                      -b, --blobs include large objects in dump
                      -c, --clean clean (drop) database objects before recreating
                      -C, --create include commands to create database in dump
                      -E, --encoding=ENCODING dump the data in encoding ENCODING
                      -n, --schema=SCHEMA dump the named schema(s) only
                      -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)
                      -o, --oids include OIDs in dump
                      -O, --no-owner skip restoration of object ownership in
                      plain-text format
                      -s, --schema-only dump only the schema, no data
                      -S, --superuser=NAME superuser user name to use in plain-text format
                      -t, --table=TABLE dump the named table(s) only
                      -T, --exclude-table=TABLE do NOT dump the named table(s)
                      -x, --no-privileges do not dump privileges (grant/revoke)
                      --binary-upgrade for use by upgrade utilities only
                      --column-inserts dump data as INSERT commands with column names
                      --disable-dollar-quoting disable dollar quoting, use SQL standard quoting
                      --disable-triggers disable triggers during data-only restore
                      --enable-row-security enable row security (dump only content user has
                      access to)
                      --exclude-table-data=TABLE do NOT dump data for the named table(s)
                      --if-exists use IF EXISTS when dropping objects
                      --inserts dump data as INSERT commands, rather than COPY
                      --no-security-labels do not dump security label assignments
                      --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
                      --no-tablespaces do not dump tablespace assignments
                      --no-unlogged-table-data do not dump unlogged table data
                      --quote-all-identifiers quote all identifiers, even if not key words
                      --section=SECTION dump named section (pre-data, data, or post-data)
                      --serializable-deferrable wait until the dump can run without anomalies
                      --snapshot=SNAPSHOT use given snapshot for the dump
                      --strict-names require table and/or schema include patterns to
                      match at least one entity each
                      --use-set-session-authorization
                      use SET SESSION AUTHORIZATION commands instead of
                      ALTER OWNER commands to set ownership

                      Connection options:
                      -d, --dbname=DBNAME database to dump
                      -h, --host=HOSTNAME database server host or socket directory
                      -p, --port=PORT database server port number
                      -U, --username=NAME connect as specified database user
                      -w, --no-password never prompt for password
                      -W, --password force password prompt (should happen automatically)
                      --role=ROLENAME do SET ROLE before dump

                      If no database name is supplied, then the PGDATABASE environment
                      variable value is used.

                      Report bugs to <pgsql-bugs@postgresql.org>.






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 2 '17 at 5:05









                      James Jithin

                      7,20032042




                      7,20032042























                          4














                          If you only want the create tables, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'






                          share|improve this answer


























                            4














                            If you only want the create tables, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'






                            share|improve this answer
























                              4












                              4








                              4






                              If you only want the create tables, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'






                              share|improve this answer












                              If you only want the create tables, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 28 '15 at 18:21









                              arod

                              4,78451724




                              4,78451724























                                  2














                                  set up a new postgresql server and replace its data folder with the files from your external disk.



                                  You will then be able to start that postgresql server up and retrieve the data using pg_dump (pg_dump -s for the schema-only as mentioned)






                                  share|improve this answer





















                                  • i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                    – programmer
                                    Jan 23 '13 at 18:44












                                  • are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                    – drone.ah
                                    Jan 23 '13 at 18:54










                                  • well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                    – programmer
                                    Jan 23 '13 at 18:59












                                  • Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                    – programmer
                                    Jan 23 '13 at 19:06








                                  • 1




                                    it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                    – drone.ah
                                    Jan 23 '13 at 19:13
















                                  2














                                  set up a new postgresql server and replace its data folder with the files from your external disk.



                                  You will then be able to start that postgresql server up and retrieve the data using pg_dump (pg_dump -s for the schema-only as mentioned)






                                  share|improve this answer





















                                  • i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                    – programmer
                                    Jan 23 '13 at 18:44












                                  • are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                    – drone.ah
                                    Jan 23 '13 at 18:54










                                  • well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                    – programmer
                                    Jan 23 '13 at 18:59












                                  • Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                    – programmer
                                    Jan 23 '13 at 19:06








                                  • 1




                                    it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                    – drone.ah
                                    Jan 23 '13 at 19:13














                                  2












                                  2








                                  2






                                  set up a new postgresql server and replace its data folder with the files from your external disk.



                                  You will then be able to start that postgresql server up and retrieve the data using pg_dump (pg_dump -s for the schema-only as mentioned)






                                  share|improve this answer












                                  set up a new postgresql server and replace its data folder with the files from your external disk.



                                  You will then be able to start that postgresql server up and retrieve the data using pg_dump (pg_dump -s for the schema-only as mentioned)







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 23 '13 at 18:25









                                  drone.ah

                                  9761228




                                  9761228












                                  • i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                    – programmer
                                    Jan 23 '13 at 18:44












                                  • are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                    – drone.ah
                                    Jan 23 '13 at 18:54










                                  • well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                    – programmer
                                    Jan 23 '13 at 18:59












                                  • Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                    – programmer
                                    Jan 23 '13 at 19:06








                                  • 1




                                    it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                    – drone.ah
                                    Jan 23 '13 at 19:13


















                                  • i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                    – programmer
                                    Jan 23 '13 at 18:44












                                  • are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                    – drone.ah
                                    Jan 23 '13 at 18:54










                                  • well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                    – programmer
                                    Jan 23 '13 at 18:59












                                  • Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                    – programmer
                                    Jan 23 '13 at 19:06








                                  • 1




                                    it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                    – drone.ah
                                    Jan 23 '13 at 19:13
















                                  i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                  – programmer
                                  Jan 23 '13 at 18:44






                                  i replaced the data folder with the old one, then i started the service, opened the pgAdmin III application, clicked on the server icon [i got only 1 server, the previous one]. Shall i create a new server? Cause it doesn't work...I still see the databases i had i created at the beginning...and not the backup ones
                                  – programmer
                                  Jan 23 '13 at 18:44














                                  are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                  – drone.ah
                                  Jan 23 '13 at 18:54




                                  are you sure you popped the files into the correct data folder? I have not used postgresql in Windows so I am not sure about where the data folder would be. I am also not sure if pgAdmin caches anything so you might need to re-connect as well...
                                  – drone.ah
                                  Jan 23 '13 at 18:54












                                  well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                  – programmer
                                  Jan 23 '13 at 18:59






                                  well i just copied the old backup data file in C:Program FilesPostgreSQL8.3 and i replaced it with the new one. Also, when i open pgAdmin again, it asks for the password of the old computer, that is a good sign but afterwards in the databases tree i don't see my databases
                                  – programmer
                                  Jan 23 '13 at 18:59














                                  Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                  – programmer
                                  Jan 23 '13 at 19:06






                                  Hm... i created a database named "db1" [a database from the old computer], when i click on it the pgAdmin outputs "An error has ocurred: FATAL cache lookup failed for database 20012", what does that mean?
                                  – programmer
                                  Jan 23 '13 at 19:06






                                  1




                                  1




                                  it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                  – drone.ah
                                  Jan 23 '13 at 19:13




                                  it looks like pgAdmin caches a lot of information. Drop your database connection in pgAdmin and re-create it. That should resolve it if the issue is around caching...
                                  – drone.ah
                                  Jan 23 '13 at 19:13











                                  0














                                  You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here






                                  share|improve this answer


























                                    0














                                    You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here






                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here






                                      share|improve this answer












                                      You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jun 25 at 16:59









                                      Hayk Petrosyan

                                      1355




                                      1355























                                          0














                                          pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>


                                          Please notice that you have sufficient privilege to access that schema.
                                          If you want take backup as specific user add user name in that command preceded by -U






                                          share|improve this answer




























                                            0














                                            pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>


                                            Please notice that you have sufficient privilege to access that schema.
                                            If you want take backup as specific user add user name in that command preceded by -U






                                            share|improve this answer


























                                              0












                                              0








                                              0






                                              pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>


                                              Please notice that you have sufficient privilege to access that schema.
                                              If you want take backup as specific user add user name in that command preceded by -U






                                              share|improve this answer














                                              pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>


                                              Please notice that you have sufficient privilege to access that schema.
                                              If you want take backup as specific user add user name in that command preceded by -U







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 21 at 10:31









                                              barbsan

                                              2,14811122




                                              2,14811122










                                              answered Nov 21 at 10:18









                                              alfons

                                              1




                                              1






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • 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.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2fstackoverflow.com%2fquestions%2f14486241%2fhow-can-i-export-the-schema-of-a-database-in-postgresql%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

                                                  404 Error Contact Form 7 ajax form submitting

                                                  How to know if a Active Directory user can login interactively

                                                  TypeError: fit_transform() missing 1 required positional argument: 'X'