IP address types (INET/CIDR)
An IP column sorted as text sorts wrong: '10.0.0.9' after '10.0.0.10'. PostgreSQL fixes this with INET; the other engines fix it with binary packing functions you have to remember to use.
- Family
- Other types
- Missing in
- SQL Server, MySQL, SQLite, Oracle
- Gotchas
- 5 listed
Engine by engine
PostgreSQL's INET and CIDR are typed, sortable and netmask-aware. MySQL packs addresses into VARBINARY with INET6_ATON; SQL Server, SQLite and Oracle store text or bytes with no IP smarts at all.
| Motor | Tipo | Almacenamiento | Notas |
|---|---|---|---|
| SQL Server |
none (VARBINARY(16) or VARCHAR(45))
Workaround
|
4-16 or up to 45 bytes | No IP type and no conversion function. Pack the address to bytes in the application (.NET IPAddress.GetAddressBytes()) or store text and accept wrong sorting. |
| PostgreSQL |
INET / CIDR
|
7 or 19 bytes (inet) | INET holds a host with optional netmask, CIDR a network; both sort correctly, support contains operators (>>), and family() distinguishes IPv4/6. |
| MySQL |
none (VARBINARY(16))
Workaround
|
4 or 16 bytes | Pack with INET6_ATON() into VARBINARY(16) (handles IPv4 and IPv6), unpack with INET6_NTOA(); the older INT + INET_ATON() is IPv4-only. |
| SQLite |
none (TEXT)
Workaround
|
up to 45 bytes | No IP type or functions. Store canonical text; sort with a custom collation or in application code if order matters. |
| Oracle |
none (VARCHAR2)
Workaround
|
up to 45 bytes | No IP type. VARCHAR2 text storage; correct ordering needs a stored binary companion column or a sort key function. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- Text-sorted IPs are misordered: '10.0.0.10' sorts before '10.0.0.9', and IPv6 text with :: compression does not sort by address at all. Any ORDER BY ip over a text column is quietly wrong.
- MySQL's INET_ATON()/INT path only fits IPv4; mixing IPv6 in requires the VARBINARY(16) + INET6_ATON() pattern, and old columns using INT cannot hold v6 at all.
- PostgreSQL CIDR rejects host bits that are set ('192.168.1.5/24'), while INET keeps them; picking the wrong one is a data-entry validation decision, not a storage one.
- The engines without a type give no validation: '999.999.1.2' inserts without complaint into VARCHAR, TEXT and NVARCHAR columns everywhere.
- IPv4-mapped IPv6 (::ffff:10.0.0.1) and 10.0.0.1 are one host in two notations, and no engine unifies them: PostgreSQL's '::ffff:10.0.0.1'::inet = '10.0.0.1'::inet is false. Normalize in the application.
Common questions
What is the difference between INET and CIDR in PostgreSQL?
INET stores one host (or network) address and accepts a netmask, keeping host bits: 10.1.2.3/8 is valid. CIDR stores a network and rejects out-of-place host bits: 10.0.0.0/8 only. Both sort and index correctly.
How should I store IPs in MySQL?
VARBINARY(16) with INET6_ATON() on write and INET6_NTOA() on read: it handles IPv4 and IPv6 in one column and sorts correctly because it is binary, not text.
Can I just store IPs as VARCHAR?
You can, and many schemas do. You lose validation, correct ordering and subnet comparisons. If any of those matter, pack to binary (or use INET in PostgreSQL) instead.
Related on SQLly
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.