Skip to content
Advertisement

Finding the intersection between two integer arrays in postgres

In postgres documentation I found that if I have two intarrays I can use the & operator to get the common elements between the two arrays(intersection), but this statement:

SELECT  ('{190,80}'::integer[]) & ('{190,80, 50}'::integer[])

raises this error:

ERROR:  operator does not exist: integer[] & integer[]
LINE 1: SELECT  (('{190,80}'::integer[]) & ('{190,80, 50}'::integer[...

I have postgresql version 13

I am wondering why doesn’t this work, and how can I fix the query or try another efficient alternative.

Advertisement

Answer

The documentation you send is for an extension. To use it you have to run CREATE EXTENSION intarray on your database so that those commands work. This will load that extension to the database and from then on you will be able to use it on all queries on that database.

You can read more about extensions here and how to load them here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement