I have a problem on PostgreSQL when asking if a given IP address (inet
type) search the right result in the database. I will provide step by step what I'm doing.
CREATE TABLE public.test (
ip inet,
a character varying
);
INSERT INTO public.test (ip, a) VALUES
('1111:0000:0101:000A:0002:0003:0004:0005', 'admin1'),
('1111:0001:0101:000A:0002:0003:0004:0005', 'admin2'),
('1111:0011:0101:000A:0002:0003:0004:0005', 'admin3'),
('1111:0111:0101:000A:0002:0003:0004:0005', 'admin4'),
('1111:1111:0101:000A:0002:0003:0004:0005', 'admin5');
Then I want search ip
by '1111:0'
, which should find the result
1111:0000:0101:000A:0002:0003:0004:0005
1111:0001:0101:000A:0002:0003:0004:0005
1111:0011:0101:000A:0002:0003:0004:0005
1111:0111:0101:000A:0002:0003:0004:0005
If you want to search for some text representation of
inet
addresses (with f.ex.LIKE
), you'll have a hard time; becauseinet
output is canonized. F.ex.:So, leading zeros are gone & and the largest block of only zeros are replaced with
::
(which is completely valid in IPv6).But, if you want to find
inet
addresses, which have 20 leading bits like1111:0
, you can use subnets.For
1111:0
, you are really looking for the child (networks / ) hosts of1111::/20
(with the contains operator:>>
):Will produce:
http://rextester.com/ZFFFK28291