This article is half-done without your Comment! *** Please share your thoughts via Comment ***
PostgreSQL provides Network Address related data types for storing information like IP Addresses, Mac Addresses.
If you are planning to store the IP Address in PostgreSQL Database, you should declare the INET data type for that column.
Internally, It checks the format of data so you cannot insert invalid format of data.
The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask.
If the /y portion is missing, the netmask is 32 for IPv4 and 128 for IPv6, so the value represents just a single host.
Small Demonstration:
Create a sample table:
1 2 3 4 5 6 |
CREATE TABLE tbl_TestNetworkDataType ( ID INT ,MyIPAddress INET ,MyMACAddress MACADDR ); |
Insert few sample Network related records:
1 2 3 4 5 6 |
INSERT INTO tbl_TestNetworkDataType VALUES (1,'10.0.2.8/16','08008b:010803') ,(2,'192.168.1.0/24','08:00:2b:01:02:03') ,(3,'150.155.124.0/32','0800.2b01.0203') ,(4,'08.26/16','08002b:010203'); |
Check the inserted data:
1 2 3 4 5 6 7 8 |
SELECT *FROM tbl_TestNetworkDataType; id | myipaddress | mymacaddress ----+----------------+------------------- 1 | 10.0.2.8/16 | 08:00:8b:01:08:03 2 | 192.168.1.0/24 | 08:00:2b:01:02:03 3 | 150.155.124.0 | 08:00:2b:01:02:03 4 | 8.26.0.0/16 | 08:00:2b:01:02:03 |
Leave a Reply