Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the equivalent of CREATE VIEW IF NOT EXISTS in postresql

I want to do something like

CREATE VIEW IF NOT EXISTS complete_user_profile ...

not sure how though

like image 880
LetsPlayYahtzee Avatar asked Feb 07 '18 11:02

LetsPlayYahtzee


People also ask

Can we create views in PostgreSQL?

Creating ViewsThe PostgreSQL views can be created from a single table, multiple tables, or another view.

How do I create a new PostgreSQL view?

To create a PostgreSQL view, we use the CREATE VIEW statement. Here is the syntax for this statement: CREATE [OR REPLACE] VIEW view-name AS SELECT column(s) FROM table(s) [WHERE condition(s)]; The OR REPLACE parameter will replace the view if it already exists.

What is a Materialised view Postgres?

A “materialized view” is a database object which stores the result of a precalculated database query and makes it easy to refresh this result as needed. Materialized views are an integral feature of pretty much all advanced database systems.


1 Answers

You could use CREATE OR REPLACE:

CREATE OR REPLACE VIEW is similar, but if a view of the same name already exists, it is replaced. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list. The calculations giving rise to the output columns may be completely different.

CREATE OR REPLACE VIEW complete_user_profile ...
like image 155
diiN__________ Avatar answered Nov 15 '22 19:11

diiN__________