PostgreSQL copy/transfer data from one database to another
This is a really straightforward task. Just use dblink for this purpose:
INSERT INTO t(a, b, c)
SELECT a, b, c FROM dblink('host=xxx user=xxx password=xxx dbname=xxx', 'SELECT a, b, c FROM t') AS x(a integer, b integer, c integer)
If you need to fetch data from external database on a regular basis, it would be wise to define a server and user mapping. Then, you could use shorter statement:
dblink('yourdbname', 'your query')
If you are on postgresql 9.0 or later (and probably 8.0 or later) in a psql session you can also use:
CREATE DATABASE new_database TEMPLATE original_database;
The new_database will be a clone of original_database including tables, table schema, encodings, and data.
From the docs:
The principal limitation is that no other sessions can be connected to the source database while it is being copied.
I would recommend that you verify that the clone is in fact correct with judicious selects from the new and old db tables. The docs also say:
It is important to understand, however, that this is not (yet) intended as a general-purpose “COPY DATABASE” facility.
There's also another way to do it. If dblink extension is not available, it's possible to copy data directly in command line, using pipe connecting standard input and ouput:
psql source_database -c 'COPY table TO stdout' | psql target_database -c 'COPY table FROM stdin'
But this is gonna work only in postgres 9.4 or higher