views:

115

answers:

3

I have two database in the same schema. My db is in Postgres. I want to copy data of any table (i.e product) of my 1st db into the same table of the 2nd db.

Is it possible to do so using query?

A: 

try

insert into db1.table1 select * from db2.table2
Yogesh
This won't work
Quassnoi
This will not work.
Faisal Feroz
A: 

It's not possible in vanilla PostgreSQL installation.

If you are able to install contrib modules, use dblink:

INSERT
INTO    product
SELECT  *
FROM    dblink
        (
        'dbname=sourcedb',
        '
        SELECT  *
        FROM    product
        '
        ) AS p (id INT, column1 INT, column2 TEXT, …)

This should be run in the target database.

Quassnoi
+3  A: 

Can't do it as a single SQL command (at least not without dblink), but the easiest way is probably to just use a pipe between two psql's - use COPY on both ends, one sending the data out in CSV format the other one receiving it.

Magnus Hagander
+1 This is indeed the easiest (and posible quickest) approach
DrColossos