Psyche
Psyche

Reputation: 8773

Export only views in Postgres

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

I'm using Postgres 8.4.

Thank you.

Upvotes: 13

Views: 23624

Answers (2)

Jan Marek
Jan Marek

Reputation: 11180

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.

Upvotes: 8

araqnid
araqnid

Reputation: 133492

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

Upvotes: 23

Related Questions