21

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 
klin
  • 112,967
  • 15
  • 204
  • 232
111
  • 1,788
  • 1
  • 23
  • 38

1 Answers1

37

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

create unique index on my_mat_view (id)
klin
  • 112,967
  • 15
  • 204
  • 232