How to insert values to one table from the table stored in another database in postgresql?
Using a Foreign Data Wrapper is generally preferable to using dblink.  Once you set up the link and import the table definition, then you can write the query just as if the table were local.

Your English description of the problem doesn't make any sense to me, so I can't tell you if the specific query you wrote does what you want.