Skip site navigation (1) Skip section navigation (2)

Re: Copy rows returned from a view into a table in a different db

From: Francisco Leovey <fleovey(at)yahoo(dot)com>
To: Thom Brown <thom(at)linux(dot)com>
Cc: "russch(at)hotmail(dot)com" <russch(at)hotmail(dot)com>, PGSQL NOVICE <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Copy rows returned from a view into a table in a different db
Date: 2011-11-08 14:29:13
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
OR ... you can write a very simple perl program
DBD:Pg allows to open 2 or more DB's and in a simple loop read from a view and insert the row in a table of a different DB 

From: Thom Brown <thom(at)linux(dot)com>
To: Russell Christopher <russch(at)hotmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Sent: Tuesday, November 8, 2011 5:24 AM
Subject: Re: [NOVICE] Copy rows returned from a view into a table in a different db

On 5 November 2011 17:55, Russell Christopher <russch(at)hotmail(dot)com> wrote:
> Complete PostgreSQL newb here.
> I have 7-8 views worth of data in db1 that I need to copy into tables with
>  matching schemae (schemas?) in a different database. The destination
> database may be the same instance of postgresql, or one on a different box
> altogether.
> I know 2-3 different ways to accomplish this with the databases I'm familiar
> with, but I'm helpless on this one. Can someone suggest some basic
> strategies for me?
> In a perfect world, I'd prefer not to have to do anything that feels too
> ETL-ish - I'd rather do some sort of SELECT FROM instance1.db1.viewname INTO
> instance2.db5.tablename then dump data out of the view as text file and
> reload into the destination table. I see postgresql has a COPY command, and
> that’ the sort of thing I want to avoid if I can.
> Since I don't know postgreSQL, so I don't really know what is within the
> realm of possibility, though.

PostgreSQL's databases are isolated from another another in a way
which means they have no visibility of other databases, so you cannot
reference a table in another database without using something like

So your options are:

1) Use the COPY command to dump the data out of your view into a text
file. (

2) Create a real table with the same structure as the view, copy the
data from the view into the table, then use pg_dump to target just
that one table, then restore it in your destination database. (see

3) Use the dblink contrib module on the destination database/server to
pull in data from the target view. (see

Hopefully when 9.2 comes out, we'll have the pgsql_fdw contrib module
which allows you to "mount" tables from other PostgreSQL databases
(local or remote) into your database, and you would be able to use
that, but unfortunately that's not available yet.


Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

EnterpriseDB UK:
The Enterprise PostgreSQL Company

In response to

pgsql-novice by date

Next:From: Steven BorrelliDate: 2011-11-09 15:21:56
Subject: psql <database> <username> <arbitrarypassword>
Previous:From: Thom BrownDate: 2011-11-08 08:24:05
Subject: Re: Copy rows returned from a view into a table in a different db

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group