Search code examples
postgresqlindexingviewmaterialized-views

Create primary key on materialized view in Postgres


How can I create a primary key on a materialized view in Postgres?

ALTER MATERIALIZED VIEW my_mat_view ADD PRIMARY KEY (id)

returns error:

Error in query: ERROR: "my_mat_view" is not a table 

Solution

  • Materialized views cannot have primary keys. You can use a unique index instead.

    create unique index on my_mat_view (id)