Thread: How to: copy records from one table to another?

How to: copy records from one table to another?

From
Dave Caughey
Date:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave

Re: How to: copy records from one table to another?

From
Calle Hedberg
Date:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
richard coleman
Date:
Dave, 

If you are talking about copying records between tables in different databases, postgresql currently doesn't support this.  If you are talking about within the same database, 'INSERT with a SELECT CLAUSE', 'SELECT INTO', and similar commands exist.

As far as I know; 
#1 Sounds good in theory, doesn't really work in practice so I can see why you can't do it in pgAdmin. 
#2 That would be a cool feature.  You can already specify the column(s) to export.  Perhaps you want to put in a feature request.
#3.Yep. that's my understanding too.  Like using dynamite to remove a splinter.  
#4 Use SQL Statements designed for this in a query window (see above).

In the end pgAdmin is nice but no substitute for some good ol' fashioned SQL.  Just open up a query tool window and go to town.

I hope that helps, 

rik.

On Fri, Mar 22, 2019 at 8:36 AM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave

Re: How to: copy records from one table to another?

From
Dave Caughey
Date:
Unfortunately, using simple SQL statements isn't an option when dealing with multiple databases (e.g., moving records from a development environment into a production system as per my proposed use case).

Cheers,
Dave


On Fri, Mar 22, 2019 at 9:18 AM Calle Hedberg <calle.hedberg@gmail.com> wrote:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
richard coleman
Date:
Dave, 

When moving data between postgresql databases, I rely on custom python scripts using psycopg2.  A simple write loop inside a read loop and two connections usually does the trick.

rik.  

On Fri, Mar 22, 2019 at 9:59 AM Dave Caughey <caugheyd@gmail.com> wrote:
Unfortunately, using simple SQL statements isn't an option when dealing with multiple databases (e.g., moving records from a development environment into a production system as per my proposed use case).

Cheers,
Dave


On Fri, Mar 22, 2019 at 9:18 AM Calle Hedberg <calle.hedberg@gmail.com> wrote:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
Dave Caughey
Date:
Yeah, similarly, for complicated migrations that impact multiple tables (e.g., needing to migrating FK's), I'll actually add a migration method to my admin Java servlet, because I can then run it in a debug mode (no commits) and have as much logging as I need to make sure that the data will be migrated correctly.  For less complicated ones, I'll just write a little Perl script that iterates over my production databases and applies the same change to each.   But for simple situations of just needing to copy rows from one table to another database, I'd been hoping there was a faster way than actually having to write some Java or Perl.

Cheers,
Dave


On Fri, Mar 22, 2019 at 10:34 AM richard coleman <rcoleman.ascentgl@gmail.com> wrote:
Dave, 

When moving data between postgresql databases, I rely on custom python scripts using psycopg2.  A simple write loop inside a read loop and two connections usually does the trick.

rik.  

On Fri, Mar 22, 2019 at 9:59 AM Dave Caughey <caugheyd@gmail.com> wrote:
Unfortunately, using simple SQL statements isn't an option when dealing with multiple databases (e.g., moving records from a development environment into a production system as per my proposed use case).

Cheers,
Dave


On Fri, Mar 22, 2019 at 9:18 AM Calle Hedberg <calle.hedberg@gmail.com> wrote:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
Patrick Headley
Date:
You could also use MS Access if you already have it. It can connect to multiple databases and allow you to run an INSERT query that can pull from one DB and save to the other.

Patrick Headley
Linx Consulting, Inc.
(303) 916-5522
pheadley@linxco-inc.com
www.linxco-inc.com
On 3/22/19 8:34 AM, richard coleman wrote:
Dave, 

When moving data between postgresql databases, I rely on custom python scripts using psycopg2.  A simple write loop inside a read loop and two connections usually does the trick.

rik.  

On Fri, Mar 22, 2019 at 9:59 AM Dave Caughey <caugheyd@gmail.com> wrote:
Unfortunately, using simple SQL statements isn't an option when dealing with multiple databases (e.g., moving records from a development environment into a production system as per my proposed use case).

Cheers,
Dave


On Fri, Mar 22, 2019 at 9:18 AM Calle Hedberg <calle.hedberg@gmail.com> wrote:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg


Re: How to: copy records from one table to another?

From
Jürgen Spatz
Date:

Dave,

I think the easiest way to copy the data from one Database to another is over foreigen data Wrappers. You could insert the configuration tables of your production database in your dev database and then simply copy the data with insert into (...) Select from ...  Command, fast and easy


Greetings

Jürgen Spatz

jurgen.spatz@godesys.de



Am 22.03.2019 um 18:34 schrieb Patrick Headley:
You could also use MS Access if you already have it. It can connect to multiple databases and allow you to run an INSERT query that can pull from one DB and save to the other.

Patrick Headley
Linx Consulting, Inc.
(303) 916-5522
pheadley@linxco-inc.com
www.linxco-inc.com
On 3/22/19 8:34 AM, richard coleman wrote:
Dave, 

When moving data between postgresql databases, I rely on custom python scripts using psycopg2.  A simple write loop inside a read loop and two connections usually does the trick.

rik.  

On Fri, Mar 22, 2019 at 9:59 AM Dave Caughey <caugheyd@gmail.com> wrote:
Unfortunately, using simple SQL statements isn't an option when dealing with multiple databases (e.g., moving records from a development environment into a production system as per my proposed use case).

Cheers,
Dave


On Fri, Mar 22, 2019 at 9:18 AM Calle Hedberg <calle.hedberg@gmail.com> wrote:
Dave,

You are talking about "copying" data from one table to another table in the same database, yes?

For that use an INSERT INTO <destinationtable> (SELECT .....FROM sourcetable) query - it will provide most of the flexibility you need with regard to including/excluding columns, renaming, changing column order, etc.

If you need to do several sequential manipulation steps on the data to be copied, it might be necessary to first insert the data into a temporary table, then do whatever manipulations necessary, before inserting the final result into the destination table (or if not absolutely necessary, at least easier - while advanced manipulation can be done in a single step, it might require advanced skills and a crystal clear understanding of the underlying processes, and doing it step by step can be safer/easier for us normal mortals...) 

Regards
Calle

On Fri, 22 Mar 2019 at 13:36, Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg



Re: How to: copy records from one table to another?

From
Khushboo Vashi
Date:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave

Re: How to: copy records from one table to another?

From
Dave Caughey
Date:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave

Re: How to: copy records from one table to another?

From
Calle Hedberg
Date:
Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
Doug Easterbrook
Date:
not sure if its been mentioned.


pg_dump -v -t (the specific table you want) which gives you the data plus the table create plus the sql

edit sql if need be 

psql   -d newdatabase < theFileAbove



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:29 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg


Re: How to: copy records from one table to another?

From
Isaias Sanchez
Date:

The first post have lot's of constraint about the ID and different order of columns, you can use copy of select's to do that stuff very easy with psql.

Example:

server_origin, db_origin, table1 (id integer, data1 text, data2 numeric, data3 jsonb)

server_destination, db_destination, table2(id integer, data4 jsonb, data5 numeric)

You need to move only part of the data from table1 to table2 (data2 -> data5 and data3 -> data4) and also filtered, you can do this:

Enter to server_destination:

psql -h server_origin -d db_origin -U anyrouser -c "copy (select data3, data2 from table1 where data1 = 'VALID') to stdout" | psql -d db_destination -c "copy table2 (data4, data5) from stdin"

Cheers,

Isaias S.

On 25/3/19 14:37, Doug Easterbrook wrote:
not sure if its been mentioned.


pg_dump -v -t (the specific table you want) which gives you the data plus the table create plus the sql

edit sql if need be 

psql   -d newdatabase < theFileAbove



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:29 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg


Re: How to: copy records from one table to another?

From
Calle Hedberg
Date:
Doug,

That's neat - I wasn't aware of that. Just tried it for a 500mb dump, and it's quick and easy - the only slightly tricker part is to edit the sql. It worked using Notepad++ for that 500MB .sql file, but less sure if it works if the file is 5 or 50 GB...

Anyway, nice to have it as an option.

Regards
Calle

On Mon, 25 Mar 2019 at 15:37, Doug Easterbrook <doug@artsman.com> wrote:
not sure if its been mentioned.


pg_dump -v -t (the specific table you want) which gives you the data plus the table create plus the sql

edit sql if need be 

psql   -d newdatabase < theFileAbove



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:29 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg




--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg

Re: How to: copy records from one table to another?

From
Doug Easterbrook
Date:
hi Calle:

love notepad ++     it works quite well for large files   but 50gig would be large.


if you know the pattern you are going after, I have used SED to edit or replace the things I don’t like, especially if it its predictable.

sed is standard unix stream editor (so available on mac and unix).

on windows, I think this is the one I’ve used http://gnuwin32.sourceforge.net/packages/sed.htm   there are others.

 
it makes playing with these files somewhat scriptable.



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:57 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Doug,

That's neat - I wasn't aware of that. Just tried it for a 500mb dump, and it's quick and easy - the only slightly tricker part is to edit the sql. It worked using Notepad++ for that 500MB .sql file, but less sure if it works if the file is 5 or 50 GB...

Anyway, nice to have it as an option.

Regards
Calle

On Mon, 25 Mar 2019 at 15:37, Doug Easterbrook <doug@artsman.com> wrote:
not sure if its been mentioned.


pg_dump -v -t (the specific table you want) which gives you the data plus the table create plus the sql

edit sql if need be 

psql   -d newdatabase < theFileAbove



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:29 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg




--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg


Re: How to: copy records from one table to another?

From
Janus
Date:
Calle,

Notepad++ isn't the best choice for very large files, but try EditPad Lite instead (I'm pretty sure it's the one I used once, although it's a few year ago), as it does some clever loading of file content. It opens even very large files in a jiffy.

Best,
Janus

On Mon, 25 Mar 2019 at 15:57, Calle Hedberg <calle.hedberg@gmail.com> wrote:
Doug,

That's neat - I wasn't aware of that. Just tried it for a 500mb dump, and it's quick and easy - the only slightly tricker part is to edit the sql. It worked using Notepad++ for that 500MB .sql file, but less sure if it works if the file is 5 or 50 GB...

Anyway, nice to have it as an option.

Regards
Calle

On Mon, 25 Mar 2019 at 15:37, Doug Easterbrook <doug@artsman.com> wrote:
not sure if its been mentioned.


pg_dump -v -t (the specific table you want) which gives you the data plus the table create plus the sql

edit sql if need be 

psql   -d newdatabase < theFileAbove



Doug Easterbrook
Arts Management Systems Ltd.
Phone (403) 650-1978

On Mar 25, 2019, at 7:29 AM, Calle Hedberg <calle.hedberg@gmail.com> wrote:

Hi

I often use CSV as a step too:
- Dump the data you want to work on, copy/edit or whatever from db 1 in csv
- Copy the SQL for the same source table design and use it to create a similar table in db 2 (using a different name where necessary)
- import the CSV data into that new table in db2
Then using the usual scripting tools to add/edit/delete the related data in db2.

I was unable to get foreign data wrappers to perform for larger data set updates recently (few hundred million records), at least when those dbs were on remote servers. Transferring a copy via CSV to use for the updates were quick and easy.

Regards
Calle

On Mon, 25 Mar 2019 at 14:04, Dave Caughey <caugheyd@gmail.com> wrote:
Hi Khushboo,

And then what's the process to upload the downloaded records into the other database?

Cheers,
Dave


On Mon, Mar 25, 2019 at 12:14 AM Khushboo Vashi <khushboo.vashi@enterprisedb.com> wrote:


On Fri, Mar 22, 2019 at 6:06 PM Dave Caughey <caugheyd@gmail.com> wrote:
Sorry, for the basic question, but I'm not sure if there are bug(s) in pgAdmin, or just that I'm clueless.  (My money lies on the latter!)

Imagine the scenario where you are adding a feature to a product that requires adding some new rows to a configuration table, and as part of the patch you need to replicate a bunch of record from your development database to your production databases.

You'd think there'd be a number of options, e.g.,

1. After doing a "View/Edit..." | "Filter by", select the displayed records, click "copy" to get them on to the clipboard, then go to the production database, do a "View/Edit..." on the corresponding table, and paste.  But, in my case, I need my auto-sequenced "id" column to be omitted (so it gets re-generated in the new table), so perhaps this isn't the right choice.  Even worse, over the years, my database tables (auto-created via Hibernate used in a Java Servlet) no longer have the same column order. (Question: is there no way that copy-and-paste between tables can consider the column names so copying between (int id,int feature_id,text name) and (int id,text name,int feature_id) is possible?)

2. Or, I could right-click on the table and use Import/Export..."  (Question: is there a way to filter the records that will get exported?   Or is there a way to trigger import/export on the results of a "View/Edit..." | "Filter by"?).  However, here the issue is the columns no longer have the same order (e.g., (int,int,text) vs (int,text,int)) so "Import/Export..." fails.  (Question: Is that not what the "Header" toggle is supposed to do?   I see that enabling it during export *adds* a header to the export files, but shouldn't enabling it during import cause it to be used to identify the order?).  This method has the attraction that I can use the "Columns" tab to exclude one of the columns from my export (i.e., my auto-sequenced "id" column).

3. Or, I could do a "Backup..." and then a corresponding "Restore..." , but I noticed that there the generated file contains CREATE DATABASE bits of code even though the "Include CREATE DATABASE" toggle in the Backup..." dialog is set to "No" (Question: bug, or my misunderstanding?).  But I'm guessing that a backup/restore will generally do a complete and utter restore, rather than just moving some data.

4. Other options?

How about Download as CSV option? 
So, what is the best/simplest way to copy data between tables, given the possibility that some/all might apply?
  • The columns may be in a different order in different databases
  • One column might need be left blank
  • I only want to copy some of the records
Cheers,
Dave


--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg




--

Carl-Anders (Calle) Hedberg

HISP

Researcher & Technical Specialist

Health Information Systems Programme – South Africa

Cell:        +47 41461011 (Norway)

Iridium SatPhone: +8816-315-19119 (usually OFF)

E-mail1: calle@hisp.org

E-mail2: calle.hedberg@gmail.com

Skype:  calle_hedberg