Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export only views in Postgres

Tags:

postgresql

Is there any way to export only the views from a Postgres schema?

I'm using Postgres 8.4.

Thank you.

like image 515
Psyche Avatar asked Dec 12 '11 11:12

Psyche


2 Answers

There's no direct flag to do this, but using our favourite query-the-schema-to-generate-a-command technique:

select string_agg( '-t ' || quote_ident(nspname) || '.' || quote_ident(relname), ' ' )
  from pg_class join pg_namespace on pg_namespace.oid = pg_class.relnamespace
  where relkind = 'v' and not (nspname ~ '^pg_' or nspname = 'information_schema');

This will generate a string that can be used with a pg_dump command, e.g.:

 -t media.duplicated_component -t adv.advert_view_distribution 

Which you could then splice into a command line directly:

pg_dump $(psql -c "select string_agg(...etc...)" db) db
like image 126
araqnid Avatar answered Nov 12 '22 06:11

araqnid


If you have every view prefixed by certain prefix, you can use this command:

pg_dump -s -t 'prefix*' dbname > db.dump

or you can use -t switch as many as possible with names of views.

See manpage for pg_dump, on the end are examples.

like image 38
Jan Marek Avatar answered Nov 12 '22 06:11

Jan Marek