I'd like to save IP address ranges in 2 columns and determine regarding some IP address if it's in this range by "greater than" and "less than" operators. Will it work if I save the ranges as VARCHAR ?
Asked
Active
Viewed 431 times
0
-
https://stackoverflow.com/questions/24944075/postgres-check-if-ip-inet-is-in-a-list-of-ip-ranges you can check if belongs to `array():inet[]` – Vao Tsun May 23 '17 at 15:26
2 Answers
2
Postgres has network address types that support those operators.

Jeffrey Chung
- 19,319
- 8
- 34
- 54
-
But I'm working with Hibernate which , by default, doesn't support mapping from/to these network addresses types. – sharon182 May 23 '17 at 15:10
-
https://stackoverflow.com/questions/20019866/hibernate-postgresql-network-address-type-inet-cdir – Jeffrey Chung May 23 '17 at 15:11
-
And if I prefer not to create a new custom class like the answer in this link, will the save as VARCHAR work ? – sharon182 May 23 '17 at 15:15
1
No, it would not work. Comparing number in string is a bad idea, as '11'
is considered smaller than '2'
(comparing the 1st char, 2 > 1, so 2nd string is greater than the 1st one). Should you not want to use the network type, you would have more luck using bigint with proper padding

JGH
- 15,928
- 4
- 31
- 48