Query to copy or transfer table data between databases with the same schema
suggest changeFirst create the extension in the database you are connected to (the one that will receive the rows):
CREATE EXTENSION dblink;
Then insert from the remote query. The AS list must give every output column its own name and type, in the same order as the remote SELECT:
INSERT INTO
<SCHEMA_NAME>.<TABLE_NAME_1>
SELECT *
FROM
dblink(
'host=<IP-ADDRESS> user=<USERNAME> password=<PASSWORD> dbname=<DATABASE>',
'SELECT * FROM <SCHEMA_NAME>.<TABLE_NAME_2>')
AS <TABLE_NAME>
(
<COLUMN_1> <DATATYPE_1>,
<COLUMN_2> <DATATYPE_2>,
<COLUMN_3> <DATATYPE_3>
);
dblink runs as the local user and opens a second session on the remote server. The column lists of the two tables have to match the SELECT * lists, including column order.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents