Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping all views in postgreSql

It seems a pretty simple problem but I can't find an answer to it! How can you delete views in bulk from the postgreSQL console? I have got 10,000 views that I made just to test something and now I can't get rid of them!

like image 752
samach Avatar asked Aug 22 '12 14:08

samach


People also ask

How do I drop a view in PostgreSQL?

The syntax for the DROP VIEW statement in PostgreSQL is: DROP VIEW [IF EXISTS] view_name; view_name. The name of the view that you wish to drop.

How do I drop all tables in PostgreSQL?

How to drop all tables in PostgreSQL? 2 From TablePlus GUI: You can select all the available tables from the right sidebar, right click and choose Delete.. , or press Delete key to drop all. Don't forget to commit changes to the server (Cmd + S) after doing so.

What does \d do in PSQL?

The command \d in psql lists all tables, views, and sequences.

What is $$ in PostgreSQL?

It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).


2 Answers

you can select the views from the meta tables, like this, (the actual select may differ if you use older version, see here e.g. http://www.alberton.info/postgresql_meta_info.html)

SELECT 'DROP VIEW ' || table_name || ';'
  FROM information_schema.views
 WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
   AND table_name !~ '^pg_';

So you fix this select according your actual version, run it, save the results into a .sql file, and run the .sql file.

like image 66
bpgergo Avatar answered Sep 20 '22 20:09

bpgergo


I add this answer which references a comment by @greg which in turn references an answer, which seems to be gone, from a user named justbob. This comment helped me a lot but might easily be overlooked by others.

Although it doesn't answer samachs question, because his views were already made, it might help others like me who found this question after a google search for a way to delete all views in a bulk operation.

I have a few views which are dependent on another and are generated by an ever-evolving script. So there will be some views added or removed from the script. To limit the places where I have to make changes when adding or removing views I want to drop them all at the beginning of the script before I create them again.

The solution I use now is to create all views in a separate schema which holds nothing but the views.

At the beginning of my script, I have the following statements before creating my views:

DROP SCHEMA IF EXISTS dbview_schema CASCADE;
CREATE SCHEMA dbview_schema;

After that, I recreate all my views in their own schema:

CREATE VIEW dbview_schema.my_view AS
  SELECT * FROM my_table
  ORDER BY my_table.my_column;
like image 40
coderuby Avatar answered Sep 20 '22 20:09

coderuby