Search code examples
postgresqlpostgresql-9.4postgresql-9.2postgresql-9.5cidr

How to find Start and End IP address for CIDR input in PostgreSQL


I have CIDR input as 10.0.0.0/28 How can i find Start and End IP address in that input. Manually i understood that 10.0.0.0 is start address and 10.0.0.15 is End IP, Is there any way in PostgreSQL to find that?


Solution

  • In you question 10.0.0.10/28 is a wrong CIDR Input. It should be 10.0.0.0/28 You can write your query like this:

    select 
    host('10.0.0.0/28'::cidr) AS lower,
    host(broadcast('10.0.0.0/28'::cidr)) AS upper 
    

    Updated Answer - if Input is as inet then try this way

    select 
    host(network('10.0.0.10/28'::inet)) AS lower,
    host(broadcast(network('10.0.0.10/28'::inet)::cidr)) AS upper 
    

    DEMO