1

I have two databases on the same server and need to copy data from a table in the first db to a table in the second. A few caveats:

  • Both tables already exist (ie: I must not drop the 'copy-to' table first. I need to just add the data to the existing table)
  • The column names differ. So I need to specify exactly which columns to copy, and what their names are in the new table

After some digging I have only been able to find this:

pg_dump -t tablename dbname | psql otherdbname 

But the above command doesn't take into account the two caveats I listed.

1
  • Use a foreign data wrapper Commented Jun 2, 2016 at 11:42

2 Answers 2

2

For a table t, with columns a and b in the source database, and x and y in the target:

psql -d sourcedb -c "copy t(a,b) to stdout" | psql -d targetdb -c "copy t(x,y) from stdin"
Sign up to request clarification or add additional context in comments.

Comments

0

I'd use an ETL tool for this. There are free tools available, they can help you change column names and they are widely used and tested. Most tools allow external schedulers like the windows task scheduler or cron to run transformations based on whatever time schedule you need.

I personally have used Pentaho PDI for similar tasks in the past and it has always worked well for me. For your requirement I'd create a single transformation that first loads the table data from the source database, modify the column names in a "Select Values"-step and then insert the values into the target table using the "truncate" option to remove the existing rows from the target table. If your table is too big to be re-filled each time, you'd need to figure out a delta load procedure.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.