HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

pg_restore a view

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
pg_restoreviewstackoverflow

Problem

What's the best way to restore a view?

  • pg_dump -h hostname -d databasename -Fc > db.dump



  • DROP VIEW viewname;



  • pg_restore -h hostname -d databasename -t viewname db.dump This does not error, but also doesn't seem to work. Have also tried it with the -n schema option. In verbose mode, it says pg_resore: implied data-only restore.



Is it possible to restore a view via pg_restore

Solution

In PostgreSQL 9.4 or older there does not appear to be a way to do this.

Here's the issue report I just posted.

If you want to patch pg_restore it looks pretty trivial, just add checking for under the test for ropt->selTypes in _tocEntryRequired (in pg_backup_archiver.c), changing:

strcmp(te->desc, "TABLE DATA") == 0)


to

strcmp(te->desc, "TABLE DATA") == 0 ||
                   strcmp(te->desc, "VIEW") == 0)


Here's the full patch: https://gist.github.com/ringerc/1743cfad34694fc9b9a3

You don't have to compile and install all of PostgreSQL for this, you can just compile a custom pg_restore if you want.

update: Here's the -hackers post with the proposed patch.

update2: This is now fixed in 9.6 according to the docs

Code Snippets

strcmp(te->desc, "TABLE DATA") == 0)
strcmp(te->desc, "TABLE DATA") == 0 ||
                   strcmp(te->desc, "VIEW") == 0)

Context

StackExchange Database Administrators Q#96729, answer score: 2

Revisions (0)

No revisions yet.