Re: Restore data to an existing populated table

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Renato Oliveira" <renato(dot)oliveira(at)grant(dot)co(dot)uk>, "pgsql-admin" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Restore data to an existing populated table
Date: 2010-04-08 16:55:36
Message-ID: 4BBDC43802000025000305A9@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Renato Oliveira <renato(dot)oliveira(at)grant(dot)co(dot)uk> wrote:

> I would like to know if it is possible to restore data to a
> pre-populated db/table.

Yes.

> Does that mean the table can exist and can be populated with
> data, pg_restore will append the data to the db/table without
> deleting/dropping the existing data?

Unless you explicitly use the "clean" option, yes.

> What I need to achieve is:
> 1 - Download the schema from an old DB

pg_dump -s

> 2 - Restore the schema to a new DB

psql or pg_restore (depending on dump format)

> 3 - Point my application to the new DB, it will populate with new
> data
> 4 - at later date do a pg_dump to dump the data from old DB -
> (Should I use 'COPY' statement instead of pg_dump?

pg_dump -a
(It will use COPY statements.)

> 5 - Restore the data only to the new DB - as in copying the data
> and appending it to existing db/tables.

psql or pg_restore (depending on dump format)
(You'll have problems if there are duplicates on primary key or
unique indexes or constraints.)

> What is the natural behaviour of pg_restore, does it wipe the
> existing data on the existing DB, or it displays 'errors' and
> carries on copying the data?

It never destroys data unless you explicitly tell it to do so. If
it hits and error attempting to add data to a table (due to schema
mismatch or duplicate rows, for example) the entire copy in to the
table fails.

I hope this helps.

-Kevin

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message David Bear 2010-04-08 18:47:51 failure on system update for lack of gpg key
Previous Message Kevin Grittner 2010-04-08 16:36:27 Re: WARM standby with pg_standby