Re: Restore 1 Table from pg_dumpall? - Mailing list pgsql-general

From Steve Crawford
Subject Re: Restore 1 Table from pg_dumpall?
Date
Msg-id 50FEE565.6030605@pinpointresearch.com
Whole thread Raw
In response to Restore 1 Table from pg_dumpall?  (Rich Shepard <rshepard@appl-ecosys.com>)
List pgsql-general
On 01/22/2013 09:57 AM, Rich Shepard wrote:
>   I neglected to dump a single table before adding additional rows to
> it via
> psql. Naturally, I messed up the table. I have a full pg_dumpall of all
> three databases and all their tables in a single .sql file from 2 days
> ago.
> The file is 386M in size and emacs is taking a very long time to move
> around
> in it.
>
>   Is there a way I can extract a single table's schema and data from the
> full backup? If so, I can then drop the fubar'd table and do it correctly
> this time.
>

If you dump using the custom format you can use pg_restore to output the
schema, data or both for a specified table.

If you have the basic text dump, then sed works reasonably well:

Table definition:
sed -n '/^CREATE TABLE yourtable (/,/^);/p' yourdump > tableschema.psql

Data:
sed -n '/^COPY yourtable (/,/^\\\./p' yourdump > tabledata.psql

This is imperfect as it doesn't include the various indexes,
constraints, sequences, etc. but it gets you most of the way there. You
can probably extract the relevant alterations with:
sed -n '/^ALTER TABLE .*yourtable$/,/;$/p'

And you can grep for index creation.

Cheers,
Steve



pgsql-general by date:

Previous
From: Vincent Veyron
Date:
Subject: Re: PostgreS Security Concern
Next
From: Edson Richter
Date:
Subject: Re: What is the impact of "varchar_pattern_ops" on performance and/or memory