<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Network Address Types</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.0.11 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Data Types" HREF="datatype.html"><LINK REL="PREVIOUS" TITLE="Geometric Types" HREF="datatype-geometric.html"><LINK REL="NEXT" TITLE="Bit String Types" HREF="datatype-bit.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2007-02-02T03:57:22"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.0.11 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="datatype-geometric.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="datatype.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 8. Data Types</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="datatype.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="datatype-bit.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="DATATYPE-NET-TYPES" >8.8. Network Address Types</A ></H1 ><A NAME="AEN5138" ></A ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > offers data types to store IPv4, IPv6, and MAC addresses, as shown in <A HREF="datatype-net-types.html#DATATYPE-NET-TYPES-TABLE" >Table 8-17</A >. It is preferable to use these types instead of plain text types to store network addresses, because these types offer input error checking and several specialized operators and functions (see <A HREF="functions-net.html" >Section 9.11</A >). </P ><DIV CLASS="TABLE" ><A NAME="DATATYPE-NET-TYPES-TABLE" ></A ><P ><B >Table 8-17. Network Address Types</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Storage Size</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="TYPE" >cidr</TT ></TD ><TD >12 or 24 bytes</TD ><TD >IPv4 and IPv6 networks</TD ></TR ><TR ><TD ><TT CLASS="TYPE" >inet</TT ></TD ><TD >12 or 24 bytes</TD ><TD >IPv4 and IPv6 hosts and networks</TD ></TR ><TR ><TD ><TT CLASS="TYPE" >macaddr</TT ></TD ><TD >6 bytes</TD ><TD >MAC addresses</TD ></TR ></TBODY ></TABLE ></DIV ><P > When sorting <TT CLASS="TYPE" >inet</TT > or <TT CLASS="TYPE" >cidr</TT > data types, IPv4 addresses will always sort before IPv6 addresses, including IPv4 addresses encapsulated or mapped into IPv6 addresses, such as ::10.2.3.4 or ::ffff::10.4.3.2. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="DATATYPE-INET" >8.8.1. <TT CLASS="TYPE" >inet</TT ></A ></H2 ><A NAME="AEN5175" ></A ><P > The <TT CLASS="TYPE" >inet</TT > type holds an IPv4 or IPv6 host address, and optionally the identity of the subnet it is in, all in one field. The subnet identity is represented by stating how many bits of the host address represent the network address (the <SPAN CLASS="QUOTE" >"netmask"</SPAN >). If the netmask is 32 and the address is IPv4, then the value does not indicate a subnet, only a single host. In IPv6, the address length is 128 bits, so 128 bits specify a unique host address. Note that if you want to accept networks only, you should use the <TT CLASS="TYPE" >cidr</TT > type rather than <TT CLASS="TYPE" >inet</TT >. </P ><P > The input format for this type is <TT CLASS="REPLACEABLE" ><I >address/y</I ></TT > where <TT CLASS="REPLACEABLE" ><I >address</I ></TT > is an IPv4 or IPv6 address and <TT CLASS="REPLACEABLE" ><I >y</I ></TT > is the number of bits in the netmask. If the <TT CLASS="REPLACEABLE" ><I >/y</I ></TT > part is left off, then the netmask is 32 for IPv4 and 128 for IPv6, so the value represents just a single host. On display, the <TT CLASS="REPLACEABLE" ><I >/y</I ></TT > portion is suppressed if the netmask specifies a single host. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="DATATYPE-CIDR" >8.8.2. <TT CLASS="TYPE" >cidr</TT ></A ></H2 ><A NAME="AEN5191" ></A ><P > The <TT CLASS="TYPE" >cidr</TT > type holds an IPv4 or IPv6 network specification. Input and output formats follow Classless Internet Domain Routing conventions. The format for specifying networks is <TT CLASS="REPLACEABLE" ><I >address/y</I ></TT > where <TT CLASS="REPLACEABLE" ><I >address</I ></TT > is the network represented as an IPv4 or IPv6 address, and <TT CLASS="REPLACEABLE" ><I >y</I ></TT > is the number of bits in the netmask. If <TT CLASS="REPLACEABLE" ><I >y</I ></TT > is omitted, it is calculated using assumptions from the older classful network numbering system, except that it will be at least large enough to include all of the octets written in the input. It is an error to specify a network address that has bits set to the right of the specified netmask. </P ><P > <A HREF="datatype-net-types.html#DATATYPE-NET-CIDR-TABLE" >Table 8-18</A > shows some examples. </P ><DIV CLASS="TABLE" ><A NAME="DATATYPE-NET-CIDR-TABLE" ></A ><P ><B >Table 8-18. <TT CLASS="TYPE" >cidr</TT > Type Input Examples</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH ><TT CLASS="TYPE" >cidr</TT > Input</TH ><TH ><TT CLASS="TYPE" >cidr</TT > Output</TH ><TH ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >abbrev</CODE >(<TT CLASS="TYPE" >cidr</TT >)</TT ></TH ></TR ></THEAD ><TBODY ><TR ><TD >192.168.100.128/25</TD ><TD >192.168.100.128/25</TD ><TD >192.168.100.128/25</TD ></TR ><TR ><TD >192.168/24</TD ><TD >192.168.0.0/24</TD ><TD >192.168.0/24</TD ></TR ><TR ><TD >192.168/25</TD ><TD >192.168.0.0/25</TD ><TD >192.168.0.0/25</TD ></TR ><TR ><TD >192.168.1</TD ><TD >192.168.1.0/24</TD ><TD >192.168.1/24</TD ></TR ><TR ><TD >192.168</TD ><TD >192.168.0.0/24</TD ><TD >192.168.0/24</TD ></TR ><TR ><TD >128.1</TD ><TD >128.1.0.0/16</TD ><TD >128.1/16</TD ></TR ><TR ><TD >128</TD ><TD >128.0.0.0/16</TD ><TD >128.0/16</TD ></TR ><TR ><TD >128.1.2</TD ><TD >128.1.2.0/24</TD ><TD >128.1.2/24</TD ></TR ><TR ><TD >10.1.2</TD ><TD >10.1.2.0/24</TD ><TD >10.1.2/24</TD ></TR ><TR ><TD >10.1</TD ><TD >10.1.0.0/16</TD ><TD >10.1/16</TD ></TR ><TR ><TD >10</TD ><TD >10.0.0.0/8</TD ><TD >10/8</TD ></TR ><TR ><TD >10.1.2.3/32</TD ><TD >10.1.2.3/32</TD ><TD >10.1.2.3/32</TD ></TR ><TR ><TD >2001:4f8:3:ba::/64</TD ><TD >2001:4f8:3:ba::/64</TD ><TD >2001:4f8:3:ba::/64</TD ></TR ><TR ><TD >2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</TD ><TD >2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</TD ><TD >2001:4f8:3:ba:2e0:81ff:fe22:d1f1</TD ></TR ><TR ><TD >::ffff:1.2.3.0/120</TD ><TD >::ffff:1.2.3.0/120</TD ><TD >::ffff:1.2.3/120</TD ></TR ><TR ><TD >::ffff:1.2.3.0/128</TD ><TD >::ffff:1.2.3.0/128</TD ><TD >::ffff:1.2.3.0/128</TD ></TR ></TBODY ></TABLE ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="DATATYPE-INET-VS-CIDR" >8.8.3. <TT CLASS="TYPE" >inet</TT > vs. <TT CLASS="TYPE" >cidr</TT ></A ></H2 ><P > The essential difference between <TT CLASS="TYPE" >inet</TT > and <TT CLASS="TYPE" >cidr</TT > data types is that <TT CLASS="TYPE" >inet</TT > accepts values with nonzero bits to the right of the netmask, whereas <TT CLASS="TYPE" >cidr</TT > does not. </P ><DIV CLASS="TIP" ><BLOCKQUOTE CLASS="TIP" ><P ><B >Tip: </B > If you do not like the output format for <TT CLASS="TYPE" >inet</TT > or <TT CLASS="TYPE" >cidr</TT > values, try the functions <CODE CLASS="FUNCTION" >host</CODE >, <CODE CLASS="FUNCTION" >text</CODE >, and <CODE CLASS="FUNCTION" >abbrev</CODE >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="DATATYPE-MACADDR" >8.8.4. <TT CLASS="TYPE" >macaddr</TT ></A ></H2 ><A NAME="AEN5299" ></A ><A NAME="AEN5301" ></A ><P > The <TT CLASS="TYPE" >macaddr</TT > type stores MAC addresses, i.e., Ethernet card hardware addresses (although MAC addresses are used for other purposes as well). Input is accepted in various customary formats, including <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><TT CLASS="LITERAL" >'08002b:010203'</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >'08002b-010203'</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >'0800.2b01.0203'</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >'08-00-2b-01-02-03'</TT ></TD ></TR ><TR ><TD ><TT CLASS="LITERAL" >'08:00:2b:01:02:03'</TT ></TD ></TR ></TBODY ></TABLE ><P ></P > which would all specify the same address. Upper and lower case is accepted for the digits <TT CLASS="LITERAL" >a</TT > through <TT CLASS="LITERAL" >f</TT >. Output is always in the last of the forms shown. </P ><P > The directory <TT CLASS="FILENAME" >contrib/mac</TT > in the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > source distribution contains tools that can be used to map MAC addresses to hardware manufacturer names. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="datatype-geometric.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="datatype-bit.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Geometric Types</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="datatype.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Bit String Types</TD ></TR ></TABLE ></DIV ></BODY ></HTML >