Backup a sample of a PostgreSQL databaseExport a subset of data, from mysql production database, to make a...

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?

Finding all intervals that match predicate in vector

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Implement the Thanos sorting algorithm

Can I Retrieve Email Addresses from BCC?

How can I use the arrow sign in my bash prompt?

Is the destination of a commercial flight important for the pilot?

Why "be dealt cards" rather than "be dealing cards"?

Increase performance creating Mandelbrot set in python

Teaching indefinite integrals that require special-casing

What is the oldest known work of fiction?

Efficiently merge handle parallel feature branches in SFDX

Hide Select Output from T-SQL

Bash method for viewing beginning and end of file

How will losing mobility of one hand affect my career as a programmer?

What's a natural way to say that someone works somewhere (for a job)?

Is it correct to write "is not focus on"?

Is it okay / does it make sense for another player to join a running game of Munchkin?

How do I keep an essay about "feeling flat" from feeling flat?

quarter to five p.m

Should my PhD thesis be submitted under my legal name?

Minimal reference content

How do I define a right arrow with bar in LaTeX?



Backup a sample of a PostgreSQL database


Export a subset of data, from mysql production database, to make a test databasePostgreSQL 9.1 Hot Backup Error: the database system is starting upHow to connect to an remote PostgreSQL database on Ubuntu using pgAdmin3?Attacks on Postgresql listening for requests on 'localhost'Connecting pgAdmin3 to Postgres on Herokupg_restore: [archiver (db)] could not execute query: ERROR: schema “public” already existspgAgent : how to configure pgpass.conf for a local connection on Windows Server 2008Remote access to Database PostgreSql on NAS Synology DS216jvHow to create a new database in PostgreSQL 10 in Windows from batch file?Postgresql 10: Can not establish DB connection













6















Is it possible to back up a PostgreSQL database with only a fraction of the data (1000 Rows from each table)?



I am able to back up that database using pg_dump. In some case I need db with minimum data.



pg_dump --host=localhost --port=5432 --username=postgres --password 
--column-inserts --schema=test testdb > test_backup.sql


How can I modify the above command to take the back up with 1000 number of data??










share|improve this question















migrated from stackoverflow.com Dec 21 '12 at 0:02


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



















  • That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

    – a_horse_with_no_name
    Dec 20 '12 at 9:53
















6















Is it possible to back up a PostgreSQL database with only a fraction of the data (1000 Rows from each table)?



I am able to back up that database using pg_dump. In some case I need db with minimum data.



pg_dump --host=localhost --port=5432 --username=postgres --password 
--column-inserts --schema=test testdb > test_backup.sql


How can I modify the above command to take the back up with 1000 number of data??










share|improve this question















migrated from stackoverflow.com Dec 21 '12 at 0:02


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



















  • That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

    – a_horse_with_no_name
    Dec 20 '12 at 9:53














6












6








6








Is it possible to back up a PostgreSQL database with only a fraction of the data (1000 Rows from each table)?



I am able to back up that database using pg_dump. In some case I need db with minimum data.



pg_dump --host=localhost --port=5432 --username=postgres --password 
--column-inserts --schema=test testdb > test_backup.sql


How can I modify the above command to take the back up with 1000 number of data??










share|improve this question
















Is it possible to back up a PostgreSQL database with only a fraction of the data (1000 Rows from each table)?



I am able to back up that database using pg_dump. In some case I need db with minimum data.



pg_dump --host=localhost --port=5432 --username=postgres --password 
--column-inserts --schema=test testdb > test_backup.sql


How can I modify the above command to take the back up with 1000 number of data??







postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 21 '12 at 0:13









ypercubeᵀᴹ

77.8k11135218




77.8k11135218










asked Dec 20 '12 at 8:00







Abhin Raj











migrated from stackoverflow.com Dec 21 '12 at 0:02


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









migrated from stackoverflow.com Dec 21 '12 at 0:02


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















  • That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

    – a_horse_with_no_name
    Dec 20 '12 at 9:53



















  • That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

    – a_horse_with_no_name
    Dec 20 '12 at 9:53

















That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

– a_horse_with_no_name
Dec 20 '12 at 9:53





That's not possible with Postgres. You might want to take a look at Jailer which promises to be able to do this honoring FK constraints: sourceforge.net/projects/jailer

– a_horse_with_no_name
Dec 20 '12 at 9:53










6 Answers
6






active

oldest

votes


















2














As stated in the other answers, you cannot do this with pg_dump. And there is an additional problem, too: if you have foreign keys between your tables, you have to retain the corresponding rows.



However, besides third-party tools (one of them was suggested by a_horse_with_no_name above), I would try the following:




  1. create a full dump

  2. before restoring, change the foreign key definitions (if necessary) as ON DELETE CASCADE - you can achieve this by a well targeted sed command, for example

  3. restore to a new database

  4. identify the tables which has foreign key dependencies

  5. delete from those everything except the rows to be retained, this will cascade to the dependencies

  6. now you have a database with the desired data only, so make a data-only dump

  7. create a schema-only dump from the original database, restore it to a new database

  8. restore the data-only dump to this database

  9. if this didn't work as expected, blame me


I haven't tried it yet, any suggestions or corrections are welcome. I see a problematic point in the 4th point: if your dependency tree consist disjoint subtrees, finding the corresponding row sets in all of them can be tricky.






share|improve this answer































    2














    I had a similar problem, I wanted to copy the most recent rows from a few tables from one db to another hosted on a different server, and I ended up writing a bash script that executes the pg_dump command followed by various psql commands:



    #!/bin/bash
    declare -a arr=("my_table_1" "my_table_2" "my_table_3")
    startdate=2014-12-01
    enddate=2014-12-30
    for table in "${arr[@]}"
    do
    echo -e \n$table\n$(for each in $(seq 1 ${#table}); do printf "-"; done)
    pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c | psql -h host_name_2 -U my_username db_name_2
    psql -h host_name_1 -U my_username db_name_1 -c "copy (select * from $table where date >='$startdate' and date <='$enddate' order by date desc) to '/tmp/data.csv' csv header;"
    psql -h host_name_2 -U my_username db_name_2 -c "copy $table from '/tmp/data.csv' csv header"
    done


    Line by line in the body of the loop:




    1. the echo is just prettyfication: it prints the name of the table and underlines it like a title,

    2. the pg_dump command generates some SQL to copy the table schema from the source and performs a few more SQL housekeeping commands (e.g. the -c flag means that the SQL will contain a command to delete the table first before creating it), and this SQL is piped with | to psql pointing at the destination (you can try running just the pg_dump part at the comand line, pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c, and see the SQL it generates),

    3. the psql on the next line copies a table in the source db into a csv file,

    4. the final psql copies from that csv file into a table in the destination db.






    share|improve this answer































      1














      You can not achieve this only with pg_dump command.



      You can do it by:



      Dump only the structure of the whole database, and use the copy command to save 1000 row from eatch table.



      For example:
      dump only structure:



      pg_dump --host=localhost --port=5432 --username=postgres --password --schema-only  testdb > test_backup.sql


      And list of copy commands called from stored procedure:



      CREATE OR REPLACE FUNCTION _save_top_1000_row_tables(chemin file_path)
      RETURNS character varying AS
      $BODY$declare
      _temps timestamp without time zone;
      begin
      execute 'copy (SELECT * FROM table1 limit 1000 offset 0) TO ''' || file_path||'table1.txt''';
      execute 'copy (SELECT * FROM table2 limit 1000 offset 0) TO ''' || file_path||'table2.txt''';
      execute 'copy (SELECT * FROM table3 limit 1000 offset 0) TO ''' || file_path||'table3.txt''';

      return ('OK');
      end;$BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;


      References:
      dump, copy






      share|improve this answer


























      • But that won't take foreign keys into account....

        – a_horse_with_no_name
        Dec 20 '12 at 9:52











      • Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

        – Houari
        Dec 20 '12 at 10:01











      • You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

        – dezso
        Dec 21 '12 at 2:56



















      0














      I think you cannot just export top NNN rows per table using only pg_dump, but you probably may write some awk script in order to filter out the unrequested records. Once you tested the awk script, you may just pipe its input from pg_dump, so you will not have temporary large file for dump. Sadly, you still will dump all your data.



      This may be used as a simple AWK you may use as an example:



      BEGIN {incopy=0; FS=""}
      incopy==0 && $0~/^COPY .*$/ { print; incopy=1; counter=0; next}
      incopy==1 && $0~/^\.$/ { print; incopy=0; next}
      incopy==1 && counter>1000 {next}
      incopy==1 {counter++}
      {print}


      please note that this does not work if your first column on any record starts with "COPY ".



      Usage: pg_dump databasename | awk -f scriptname > dumpfile





      share|improve this answer


























      • This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

        – dezso
        Dec 21 '12 at 10:11











      • In the same way: you get errors :-)

        – eppesuig
        Dec 21 '12 at 10:12



















      0














      I had the same problem recently and I couldn't find any sufficiently good solution. So I created a small tool to dump a sample of data from the PostgreSQL database: https://github.com/dankeder/pg_dump_sample.






      share|improve this answer































        0














        To what it worth, this is the way I did it.



        I've used pg_dump normally to dump the whole table. Then, I used head to load only part of it:



        head sample_table.sql -n 1000 > sample_table.1000.sql
        psql -h localhost -U postgres my-db < sample_table.1000.sql


        It does not cut exactly 1000 records. In my case it was 952 (because there are number of lines at the top of the file)






        share|improve this answer








        New contributor




        Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          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%2f30869%2fbackup-a-sample-of-a-postgresql-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown
























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          As stated in the other answers, you cannot do this with pg_dump. And there is an additional problem, too: if you have foreign keys between your tables, you have to retain the corresponding rows.



          However, besides third-party tools (one of them was suggested by a_horse_with_no_name above), I would try the following:




          1. create a full dump

          2. before restoring, change the foreign key definitions (if necessary) as ON DELETE CASCADE - you can achieve this by a well targeted sed command, for example

          3. restore to a new database

          4. identify the tables which has foreign key dependencies

          5. delete from those everything except the rows to be retained, this will cascade to the dependencies

          6. now you have a database with the desired data only, so make a data-only dump

          7. create a schema-only dump from the original database, restore it to a new database

          8. restore the data-only dump to this database

          9. if this didn't work as expected, blame me


          I haven't tried it yet, any suggestions or corrections are welcome. I see a problematic point in the 4th point: if your dependency tree consist disjoint subtrees, finding the corresponding row sets in all of them can be tricky.






          share|improve this answer




























            2














            As stated in the other answers, you cannot do this with pg_dump. And there is an additional problem, too: if you have foreign keys between your tables, you have to retain the corresponding rows.



            However, besides third-party tools (one of them was suggested by a_horse_with_no_name above), I would try the following:




            1. create a full dump

            2. before restoring, change the foreign key definitions (if necessary) as ON DELETE CASCADE - you can achieve this by a well targeted sed command, for example

            3. restore to a new database

            4. identify the tables which has foreign key dependencies

            5. delete from those everything except the rows to be retained, this will cascade to the dependencies

            6. now you have a database with the desired data only, so make a data-only dump

            7. create a schema-only dump from the original database, restore it to a new database

            8. restore the data-only dump to this database

            9. if this didn't work as expected, blame me


            I haven't tried it yet, any suggestions or corrections are welcome. I see a problematic point in the 4th point: if your dependency tree consist disjoint subtrees, finding the corresponding row sets in all of them can be tricky.






            share|improve this answer


























              2












              2








              2







              As stated in the other answers, you cannot do this with pg_dump. And there is an additional problem, too: if you have foreign keys between your tables, you have to retain the corresponding rows.



              However, besides third-party tools (one of them was suggested by a_horse_with_no_name above), I would try the following:




              1. create a full dump

              2. before restoring, change the foreign key definitions (if necessary) as ON DELETE CASCADE - you can achieve this by a well targeted sed command, for example

              3. restore to a new database

              4. identify the tables which has foreign key dependencies

              5. delete from those everything except the rows to be retained, this will cascade to the dependencies

              6. now you have a database with the desired data only, so make a data-only dump

              7. create a schema-only dump from the original database, restore it to a new database

              8. restore the data-only dump to this database

              9. if this didn't work as expected, blame me


              I haven't tried it yet, any suggestions or corrections are welcome. I see a problematic point in the 4th point: if your dependency tree consist disjoint subtrees, finding the corresponding row sets in all of them can be tricky.






              share|improve this answer













              As stated in the other answers, you cannot do this with pg_dump. And there is an additional problem, too: if you have foreign keys between your tables, you have to retain the corresponding rows.



              However, besides third-party tools (one of them was suggested by a_horse_with_no_name above), I would try the following:




              1. create a full dump

              2. before restoring, change the foreign key definitions (if necessary) as ON DELETE CASCADE - you can achieve this by a well targeted sed command, for example

              3. restore to a new database

              4. identify the tables which has foreign key dependencies

              5. delete from those everything except the rows to be retained, this will cascade to the dependencies

              6. now you have a database with the desired data only, so make a data-only dump

              7. create a schema-only dump from the original database, restore it to a new database

              8. restore the data-only dump to this database

              9. if this didn't work as expected, blame me


              I haven't tried it yet, any suggestions or corrections are welcome. I see a problematic point in the 4th point: if your dependency tree consist disjoint subtrees, finding the corresponding row sets in all of them can be tricky.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 21 '12 at 12:03









              dezsodezso

              22.4k116097




              22.4k116097

























                  2














                  I had a similar problem, I wanted to copy the most recent rows from a few tables from one db to another hosted on a different server, and I ended up writing a bash script that executes the pg_dump command followed by various psql commands:



                  #!/bin/bash
                  declare -a arr=("my_table_1" "my_table_2" "my_table_3")
                  startdate=2014-12-01
                  enddate=2014-12-30
                  for table in "${arr[@]}"
                  do
                  echo -e \n$table\n$(for each in $(seq 1 ${#table}); do printf "-"; done)
                  pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c | psql -h host_name_2 -U my_username db_name_2
                  psql -h host_name_1 -U my_username db_name_1 -c "copy (select * from $table where date >='$startdate' and date <='$enddate' order by date desc) to '/tmp/data.csv' csv header;"
                  psql -h host_name_2 -U my_username db_name_2 -c "copy $table from '/tmp/data.csv' csv header"
                  done


                  Line by line in the body of the loop:




                  1. the echo is just prettyfication: it prints the name of the table and underlines it like a title,

                  2. the pg_dump command generates some SQL to copy the table schema from the source and performs a few more SQL housekeeping commands (e.g. the -c flag means that the SQL will contain a command to delete the table first before creating it), and this SQL is piped with | to psql pointing at the destination (you can try running just the pg_dump part at the comand line, pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c, and see the SQL it generates),

                  3. the psql on the next line copies a table in the source db into a csv file,

                  4. the final psql copies from that csv file into a table in the destination db.






                  share|improve this answer




























                    2














                    I had a similar problem, I wanted to copy the most recent rows from a few tables from one db to another hosted on a different server, and I ended up writing a bash script that executes the pg_dump command followed by various psql commands:



                    #!/bin/bash
                    declare -a arr=("my_table_1" "my_table_2" "my_table_3")
                    startdate=2014-12-01
                    enddate=2014-12-30
                    for table in "${arr[@]}"
                    do
                    echo -e \n$table\n$(for each in $(seq 1 ${#table}); do printf "-"; done)
                    pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c | psql -h host_name_2 -U my_username db_name_2
                    psql -h host_name_1 -U my_username db_name_1 -c "copy (select * from $table where date >='$startdate' and date <='$enddate' order by date desc) to '/tmp/data.csv' csv header;"
                    psql -h host_name_2 -U my_username db_name_2 -c "copy $table from '/tmp/data.csv' csv header"
                    done


                    Line by line in the body of the loop:




                    1. the echo is just prettyfication: it prints the name of the table and underlines it like a title,

                    2. the pg_dump command generates some SQL to copy the table schema from the source and performs a few more SQL housekeeping commands (e.g. the -c flag means that the SQL will contain a command to delete the table first before creating it), and this SQL is piped with | to psql pointing at the destination (you can try running just the pg_dump part at the comand line, pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c, and see the SQL it generates),

                    3. the psql on the next line copies a table in the source db into a csv file,

                    4. the final psql copies from that csv file into a table in the destination db.






                    share|improve this answer


























                      2












                      2








                      2







                      I had a similar problem, I wanted to copy the most recent rows from a few tables from one db to another hosted on a different server, and I ended up writing a bash script that executes the pg_dump command followed by various psql commands:



                      #!/bin/bash
                      declare -a arr=("my_table_1" "my_table_2" "my_table_3")
                      startdate=2014-12-01
                      enddate=2014-12-30
                      for table in "${arr[@]}"
                      do
                      echo -e \n$table\n$(for each in $(seq 1 ${#table}); do printf "-"; done)
                      pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c | psql -h host_name_2 -U my_username db_name_2
                      psql -h host_name_1 -U my_username db_name_1 -c "copy (select * from $table where date >='$startdate' and date <='$enddate' order by date desc) to '/tmp/data.csv' csv header;"
                      psql -h host_name_2 -U my_username db_name_2 -c "copy $table from '/tmp/data.csv' csv header"
                      done


                      Line by line in the body of the loop:




                      1. the echo is just prettyfication: it prints the name of the table and underlines it like a title,

                      2. the pg_dump command generates some SQL to copy the table schema from the source and performs a few more SQL housekeeping commands (e.g. the -c flag means that the SQL will contain a command to delete the table first before creating it), and this SQL is piped with | to psql pointing at the destination (you can try running just the pg_dump part at the comand line, pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c, and see the SQL it generates),

                      3. the psql on the next line copies a table in the source db into a csv file,

                      4. the final psql copies from that csv file into a table in the destination db.






                      share|improve this answer













                      I had a similar problem, I wanted to copy the most recent rows from a few tables from one db to another hosted on a different server, and I ended up writing a bash script that executes the pg_dump command followed by various psql commands:



                      #!/bin/bash
                      declare -a arr=("my_table_1" "my_table_2" "my_table_3")
                      startdate=2014-12-01
                      enddate=2014-12-30
                      for table in "${arr[@]}"
                      do
                      echo -e \n$table\n$(for each in $(seq 1 ${#table}); do printf "-"; done)
                      pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c | psql -h host_name_2 -U my_username db_name_2
                      psql -h host_name_1 -U my_username db_name_1 -c "copy (select * from $table where date >='$startdate' and date <='$enddate' order by date desc) to '/tmp/data.csv' csv header;"
                      psql -h host_name_2 -U my_username db_name_2 -c "copy $table from '/tmp/data.csv' csv header"
                      done


                      Line by line in the body of the loop:




                      1. the echo is just prettyfication: it prints the name of the table and underlines it like a title,

                      2. the pg_dump command generates some SQL to copy the table schema from the source and performs a few more SQL housekeeping commands (e.g. the -c flag means that the SQL will contain a command to delete the table first before creating it), and this SQL is piped with | to psql pointing at the destination (you can try running just the pg_dump part at the comand line, pg_dump -h host_name_1 --schema-only -t $table db_name_1 -U my_username -c, and see the SQL it generates),

                      3. the psql on the next line copies a table in the source db into a csv file,

                      4. the final psql copies from that csv file into a table in the destination db.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 30 '14 at 13:51









                      RobertRobert

                      1212




                      1212























                          1














                          You can not achieve this only with pg_dump command.



                          You can do it by:



                          Dump only the structure of the whole database, and use the copy command to save 1000 row from eatch table.



                          For example:
                          dump only structure:



                          pg_dump --host=localhost --port=5432 --username=postgres --password --schema-only  testdb > test_backup.sql


                          And list of copy commands called from stored procedure:



                          CREATE OR REPLACE FUNCTION _save_top_1000_row_tables(chemin file_path)
                          RETURNS character varying AS
                          $BODY$declare
                          _temps timestamp without time zone;
                          begin
                          execute 'copy (SELECT * FROM table1 limit 1000 offset 0) TO ''' || file_path||'table1.txt''';
                          execute 'copy (SELECT * FROM table2 limit 1000 offset 0) TO ''' || file_path||'table2.txt''';
                          execute 'copy (SELECT * FROM table3 limit 1000 offset 0) TO ''' || file_path||'table3.txt''';

                          return ('OK');
                          end;$BODY$
                          LANGUAGE plpgsql VOLATILE
                          COST 100;


                          References:
                          dump, copy






                          share|improve this answer


























                          • But that won't take foreign keys into account....

                            – a_horse_with_no_name
                            Dec 20 '12 at 9:52











                          • Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                            – Houari
                            Dec 20 '12 at 10:01











                          • You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                            – dezso
                            Dec 21 '12 at 2:56
















                          1














                          You can not achieve this only with pg_dump command.



                          You can do it by:



                          Dump only the structure of the whole database, and use the copy command to save 1000 row from eatch table.



                          For example:
                          dump only structure:



                          pg_dump --host=localhost --port=5432 --username=postgres --password --schema-only  testdb > test_backup.sql


                          And list of copy commands called from stored procedure:



                          CREATE OR REPLACE FUNCTION _save_top_1000_row_tables(chemin file_path)
                          RETURNS character varying AS
                          $BODY$declare
                          _temps timestamp without time zone;
                          begin
                          execute 'copy (SELECT * FROM table1 limit 1000 offset 0) TO ''' || file_path||'table1.txt''';
                          execute 'copy (SELECT * FROM table2 limit 1000 offset 0) TO ''' || file_path||'table2.txt''';
                          execute 'copy (SELECT * FROM table3 limit 1000 offset 0) TO ''' || file_path||'table3.txt''';

                          return ('OK');
                          end;$BODY$
                          LANGUAGE plpgsql VOLATILE
                          COST 100;


                          References:
                          dump, copy






                          share|improve this answer


























                          • But that won't take foreign keys into account....

                            – a_horse_with_no_name
                            Dec 20 '12 at 9:52











                          • Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                            – Houari
                            Dec 20 '12 at 10:01











                          • You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                            – dezso
                            Dec 21 '12 at 2:56














                          1












                          1








                          1







                          You can not achieve this only with pg_dump command.



                          You can do it by:



                          Dump only the structure of the whole database, and use the copy command to save 1000 row from eatch table.



                          For example:
                          dump only structure:



                          pg_dump --host=localhost --port=5432 --username=postgres --password --schema-only  testdb > test_backup.sql


                          And list of copy commands called from stored procedure:



                          CREATE OR REPLACE FUNCTION _save_top_1000_row_tables(chemin file_path)
                          RETURNS character varying AS
                          $BODY$declare
                          _temps timestamp without time zone;
                          begin
                          execute 'copy (SELECT * FROM table1 limit 1000 offset 0) TO ''' || file_path||'table1.txt''';
                          execute 'copy (SELECT * FROM table2 limit 1000 offset 0) TO ''' || file_path||'table2.txt''';
                          execute 'copy (SELECT * FROM table3 limit 1000 offset 0) TO ''' || file_path||'table3.txt''';

                          return ('OK');
                          end;$BODY$
                          LANGUAGE plpgsql VOLATILE
                          COST 100;


                          References:
                          dump, copy






                          share|improve this answer















                          You can not achieve this only with pg_dump command.



                          You can do it by:



                          Dump only the structure of the whole database, and use the copy command to save 1000 row from eatch table.



                          For example:
                          dump only structure:



                          pg_dump --host=localhost --port=5432 --username=postgres --password --schema-only  testdb > test_backup.sql


                          And list of copy commands called from stored procedure:



                          CREATE OR REPLACE FUNCTION _save_top_1000_row_tables(chemin file_path)
                          RETURNS character varying AS
                          $BODY$declare
                          _temps timestamp without time zone;
                          begin
                          execute 'copy (SELECT * FROM table1 limit 1000 offset 0) TO ''' || file_path||'table1.txt''';
                          execute 'copy (SELECT * FROM table2 limit 1000 offset 0) TO ''' || file_path||'table2.txt''';
                          execute 'copy (SELECT * FROM table3 limit 1000 offset 0) TO ''' || file_path||'table3.txt''';

                          return ('OK');
                          end;$BODY$
                          LANGUAGE plpgsql VOLATILE
                          COST 100;


                          References:
                          dump, copy







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 21 '12 at 2:45









                          dezso

                          22.4k116097




                          22.4k116097










                          answered Dec 20 '12 at 9:27









                          HouariHouari

                          1465




                          1465













                          • But that won't take foreign keys into account....

                            – a_horse_with_no_name
                            Dec 20 '12 at 9:52











                          • Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                            – Houari
                            Dec 20 '12 at 10:01











                          • You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                            – dezso
                            Dec 21 '12 at 2:56



















                          • But that won't take foreign keys into account....

                            – a_horse_with_no_name
                            Dec 20 '12 at 9:52











                          • Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                            – Houari
                            Dec 20 '12 at 10:01











                          • You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                            – dezso
                            Dec 21 '12 at 2:56

















                          But that won't take foreign keys into account....

                          – a_horse_with_no_name
                          Dec 20 '12 at 9:52





                          But that won't take foreign keys into account....

                          – a_horse_with_no_name
                          Dec 20 '12 at 9:52













                          Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                          – Houari
                          Dec 20 '12 at 10:01





                          Can you update your question by giving more informations about your database shema? I agree that it's not simple if there is lot of foreign keys.

                          – Houari
                          Dec 20 '12 at 10:01













                          You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                          – dezso
                          Dec 21 '12 at 2:56





                          You'll get ERROR: must be superuser to COPY to or from a file unless you do this as a superuser.

                          – dezso
                          Dec 21 '12 at 2:56











                          0














                          I think you cannot just export top NNN rows per table using only pg_dump, but you probably may write some awk script in order to filter out the unrequested records. Once you tested the awk script, you may just pipe its input from pg_dump, so you will not have temporary large file for dump. Sadly, you still will dump all your data.



                          This may be used as a simple AWK you may use as an example:



                          BEGIN {incopy=0; FS=""}
                          incopy==0 && $0~/^COPY .*$/ { print; incopy=1; counter=0; next}
                          incopy==1 && $0~/^\.$/ { print; incopy=0; next}
                          incopy==1 && counter>1000 {next}
                          incopy==1 {counter++}
                          {print}


                          please note that this does not work if your first column on any record starts with "COPY ".



                          Usage: pg_dump databasename | awk -f scriptname > dumpfile





                          share|improve this answer


























                          • This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                            – dezso
                            Dec 21 '12 at 10:11











                          • In the same way: you get errors :-)

                            – eppesuig
                            Dec 21 '12 at 10:12
















                          0














                          I think you cannot just export top NNN rows per table using only pg_dump, but you probably may write some awk script in order to filter out the unrequested records. Once you tested the awk script, you may just pipe its input from pg_dump, so you will not have temporary large file for dump. Sadly, you still will dump all your data.



                          This may be used as a simple AWK you may use as an example:



                          BEGIN {incopy=0; FS=""}
                          incopy==0 && $0~/^COPY .*$/ { print; incopy=1; counter=0; next}
                          incopy==1 && $0~/^\.$/ { print; incopy=0; next}
                          incopy==1 && counter>1000 {next}
                          incopy==1 {counter++}
                          {print}


                          please note that this does not work if your first column on any record starts with "COPY ".



                          Usage: pg_dump databasename | awk -f scriptname > dumpfile





                          share|improve this answer


























                          • This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                            – dezso
                            Dec 21 '12 at 10:11











                          • In the same way: you get errors :-)

                            – eppesuig
                            Dec 21 '12 at 10:12














                          0












                          0








                          0







                          I think you cannot just export top NNN rows per table using only pg_dump, but you probably may write some awk script in order to filter out the unrequested records. Once you tested the awk script, you may just pipe its input from pg_dump, so you will not have temporary large file for dump. Sadly, you still will dump all your data.



                          This may be used as a simple AWK you may use as an example:



                          BEGIN {incopy=0; FS=""}
                          incopy==0 && $0~/^COPY .*$/ { print; incopy=1; counter=0; next}
                          incopy==1 && $0~/^\.$/ { print; incopy=0; next}
                          incopy==1 && counter>1000 {next}
                          incopy==1 {counter++}
                          {print}


                          please note that this does not work if your first column on any record starts with "COPY ".



                          Usage: pg_dump databasename | awk -f scriptname > dumpfile





                          share|improve this answer















                          I think you cannot just export top NNN rows per table using only pg_dump, but you probably may write some awk script in order to filter out the unrequested records. Once you tested the awk script, you may just pipe its input from pg_dump, so you will not have temporary large file for dump. Sadly, you still will dump all your data.



                          This may be used as a simple AWK you may use as an example:



                          BEGIN {incopy=0; FS=""}
                          incopy==0 && $0~/^COPY .*$/ { print; incopy=1; counter=0; next}
                          incopy==1 && $0~/^\.$/ { print; incopy=0; next}
                          incopy==1 && counter>1000 {next}
                          incopy==1 {counter++}
                          {print}


                          please note that this does not work if your first column on any record starts with "COPY ".



                          Usage: pg_dump databasename | awk -f scriptname > dumpfile






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 21 '12 at 10:11

























                          answered Dec 21 '12 at 10:02









                          eppesuigeppesuig

                          2,8071814




                          2,8071814













                          • This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                            – dezso
                            Dec 21 '12 at 10:11











                          • In the same way: you get errors :-)

                            – eppesuig
                            Dec 21 '12 at 10:12



















                          • This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                            – dezso
                            Dec 21 '12 at 10:11











                          • In the same way: you get errors :-)

                            – eppesuig
                            Dec 21 '12 at 10:12

















                          This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                          – dezso
                          Dec 21 '12 at 10:11





                          This has the same problem mentioned in a comment earlier: how do you take foreign keys into account?

                          – dezso
                          Dec 21 '12 at 10:11













                          In the same way: you get errors :-)

                          – eppesuig
                          Dec 21 '12 at 10:12





                          In the same way: you get errors :-)

                          – eppesuig
                          Dec 21 '12 at 10:12











                          0














                          I had the same problem recently and I couldn't find any sufficiently good solution. So I created a small tool to dump a sample of data from the PostgreSQL database: https://github.com/dankeder/pg_dump_sample.






                          share|improve this answer




























                            0














                            I had the same problem recently and I couldn't find any sufficiently good solution. So I created a small tool to dump a sample of data from the PostgreSQL database: https://github.com/dankeder/pg_dump_sample.






                            share|improve this answer


























                              0












                              0








                              0







                              I had the same problem recently and I couldn't find any sufficiently good solution. So I created a small tool to dump a sample of data from the PostgreSQL database: https://github.com/dankeder/pg_dump_sample.






                              share|improve this answer













                              I had the same problem recently and I couldn't find any sufficiently good solution. So I created a small tool to dump a sample of data from the PostgreSQL database: https://github.com/dankeder/pg_dump_sample.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jun 17 '16 at 16:02









                              Dan KederDan Keder

                              101




                              101























                                  0














                                  To what it worth, this is the way I did it.



                                  I've used pg_dump normally to dump the whole table. Then, I used head to load only part of it:



                                  head sample_table.sql -n 1000 > sample_table.1000.sql
                                  psql -h localhost -U postgres my-db < sample_table.1000.sql


                                  It does not cut exactly 1000 records. In my case it was 952 (because there are number of lines at the top of the file)






                                  share|improve this answer








                                  New contributor




                                  Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.

























                                    0














                                    To what it worth, this is the way I did it.



                                    I've used pg_dump normally to dump the whole table. Then, I used head to load only part of it:



                                    head sample_table.sql -n 1000 > sample_table.1000.sql
                                    psql -h localhost -U postgres my-db < sample_table.1000.sql


                                    It does not cut exactly 1000 records. In my case it was 952 (because there are number of lines at the top of the file)






                                    share|improve this answer








                                    New contributor




                                    Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.























                                      0












                                      0








                                      0







                                      To what it worth, this is the way I did it.



                                      I've used pg_dump normally to dump the whole table. Then, I used head to load only part of it:



                                      head sample_table.sql -n 1000 > sample_table.1000.sql
                                      psql -h localhost -U postgres my-db < sample_table.1000.sql


                                      It does not cut exactly 1000 records. In my case it was 952 (because there are number of lines at the top of the file)






                                      share|improve this answer








                                      New contributor




                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.










                                      To what it worth, this is the way I did it.



                                      I've used pg_dump normally to dump the whole table. Then, I used head to load only part of it:



                                      head sample_table.sql -n 1000 > sample_table.1000.sql
                                      psql -h localhost -U postgres my-db < sample_table.1000.sql


                                      It does not cut exactly 1000 records. In my case it was 952 (because there are number of lines at the top of the file)







                                      share|improve this answer








                                      New contributor




                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      share|improve this answer



                                      share|improve this answer






                                      New contributor




                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      answered 10 mins ago









                                      Roozbeh ZabihollahiRoozbeh Zabihollahi

                                      1012




                                      1012




                                      New contributor




                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.





                                      New contributor





                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.






                                      Roozbeh Zabihollahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.






























                                          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%2f30869%2fbackup-a-sample-of-a-postgresql-database%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

                                          Parapolítica Índice Antecedentes El escándalo Proceso judicial Consecuencias Véase...

                                          How to remove border from elements in the last row?Targeting flex items on the last rowHow to vertically wrap...

                                          Tecnologías entrañables Índice Antecedentes Desarrollo Tecnologías Entrañables en la...