Thread: Copying data from one table of one database to other table f other database

Copying data from one table of one database to other table f other database

From
"Preetam Palwe"
Date:

Hello all

                I want to copy the data from one table to other table. These tables reside in two different databases.

I am thinking of using following query

 

insert into table2 (select * from table1)

 

but the problem is how can I specify the database name?

 

thanks for the help

 

~PP



On Mon, Apr 27, 2009 at 6:30 AM, Preetam Palwe <preetamp@aftek.com> wrote:

Hello all

                I want to copy the data from one table to other table. These tables reside in two different databases.

I am thinking of using following query

 

insert into table2 (select * from table1)

 

but the problem is how can I specify the database name?


You'll need to look at the dblink contrib module.  Postgres does not include the ability to use cross-database queries out-of-the-box.  The better way of doing what you are talking about is to use separate schemas within the same database.

Sean
 

On 2009-04-27, Preetam Palwe <preetamp@aftek.com> wrote:
> This is a multi-part message in MIME format.

> Hello all
>
>                 I want to copy the data from one table to other table.
> These tables reside in two different databases.
>
> I am thinking of using following query=20
>
>
> insert into table2 (select * from table1)
>
>
> but the problem is how can I specify the database name?

you can't



you can do this however:

 psql -c " copy (select * from table1) to stdout " database1 | psql -c " copy table2 from stdin " database2


Thanks folks!
Following command worked for me


psql -h x.x.x.x -U postgres -d securez -c " copy  alerts to stdout "  |
psql -h x.x.x.x -U postgres -d p -c " copy alerts from stdin "

but
psql -h x.x.x.x -U postgres -d securez -c " copy (select * from alerts)
to stdout "  | psql -h x.x.x.x -U postgres -d p -c " copy alerts from
stdin "

is giving me syntax error as

ERROR:  syntax error at or near "("
LINE 1:  copy (select * from alerts) to stdout

Any idea ?



-----Original Message-----
From: pgsql-novice-owner@postgresql.org
[mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Jasen Betts
Sent: Tuesday, April 28, 2009 6:35 PM
To: pgsql-novice@postgresql.org
Subject: [NOVICE] Re: Copying data from one table of one database to
other table f other database

On 2009-04-27, Preetam Palwe <preetamp@aftek.com> wrote:
> This is a multi-part message in MIME format.

> Hello all
>
>                 I want to copy the data from one table to other table.
> These tables reside in two different databases.
>
> I am thinking of using following query=20
>
>
> insert into table2 (select * from table1)
>
>
> but the problem is how can I specify the database name?

you can't



you can do this however:

 psql -c " copy (select * from table1) to stdout " database1 | psql -c "
copy table2 from stdin " database2


--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice


On 2009-04-28, Preetam Palwe <preetamp@aftek.com> wrote:
> Thanks folks!
> Following command worked for me
>
>
> psql -h x.x.x.x -U postgres -d securez -c " copy  alerts to stdout "  |
> psql -h x.x.x.x -U postgres -d p -c " copy alerts from stdin "
>
> but
> psql -h x.x.x.x -U postgres -d securez -c " copy (select * from alerts)
> to stdout "  | psql -h x.x.x.x -U postgres -d p -c " copy alerts from
> stdin "
>
> is giving me syntax error as
>
> ERROR:  syntax error at or near "("
> LINE 1:  copy (select * from alerts) to stdout
>
> Any idea ?

That syntax needs version 8.3, I usually do it that way, but with a where
clause in the select as most times I don't want the whole table.