Storing network information

09.2013 / Category: / Tags:

Given my experience in my daily work as a PostgreSQL professional I have the impression that most people are still not aware of the fact that PostgreSQL can handle IP addresses - and network information in general - pretty nicely.

But, there are actually two data types that can help users to store and verify network data: cidr and inet. Here are some examples:

The inet type accepts an optional netmask. It is perfect to store entire IP ranges. The cidr type will only hold a single IP address.

The beauty of validation

Just like every data type in PostgreSQL, cidr and inet will validate their content nicely:

Validating data directly is important if you want to keep bad data out of your system.

Some little math

Did you know that you can actually do math in PostgreSQL? You can add and subtract numbers from an IP range easily. This is very nice if you want to find the next spare IP address or whatever.

Here is how it works:

By the way: This kind of magic will also work with IPv6 IPs:

For the most recent PostgreSQL networking posts, see the networking blog spot.

In case you need any assistance, please feel free to contact us.

In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Facebook or LinkedIn.

Comments are closed.

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf

+43 (0) 2622 93022-0

Get the newest PostgreSQL Info & Tools

    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    CYBERTEC PostgreSQL International GmbH
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram