Skip to content

MACADDR

The MACADDR data type stores 6-byte MAC addresses. (Note that fully uncompressed MACADDR values will occupy more than 6 bytes of storage in the database.)

You can insert MAC addresses into table columns in several different formats. All of the following 12-character formats are valid, using colons, periods, or hyphens as separators (or no separator). Upper and lower case is accepted for the characters a through f. All of these input values would result in the same MAC address being stored:

'08:00:2b:01:02:03'
'08-00-2b-01-02-03'
'08002b:010203'
'08002b-010203'
'0800.2b01.0203'
'0800-2b01-0203'
'08002b010203'

Output is always returned in the first format: six pairs of digits or characters, separated by colons. For example:

premdb=# create table macaddrs(c1 macaddr);
CREATE TABLE
premdb=# \d macaddrs
   Table "public.macaddrs"
 Column |  Type   | Modifiers 
--------+---------+-----------
 c1     | macaddr | 

Distribution: Hash (c1)

premdb=# insert into macaddrs 
values('08:00:2b:01:02:03'),
     ('08-00-2b-01-02-03'),
     ('08002b:010203'),
     ('08002b-010203'),
     ('0800.2b01.0203'),
     ('0800-2b01-0203'),
     ('08002b010203');
INSERT 0 7
premdb=# select * from macaddrs;
       c1         
-------------------
 08:00:2b:01:02:03
 08:00:2b:01:02:03
 08:00:2b:01:02:03
 08:00:2b:01:02:03
 08:00:2b:01:02:03
 08:00:2b:01:02:03
 08:00:2b:01:02:03
(7 rows)

To explicitly identify a string as a MACADDR literal value, use the MACADDR keyword. For example:

where mac_address > macaddr '08:00:2b:01:02:03'

MACADDR columns do not accept empty strings.

You can cast a MACADDR value to the MACADDR8 type; 6-byte addresses are stored with the 4th and 5th bytes set to FF and FE, respectively. For example:

premdb=# select * from macaddrs;
       mac        |          mac8           
-------------------+-------------------------
 08:00:2b:01:02:03 | 08:00:2b:01:02:03:04:05
(1 row)

premdb=# select mac::macaddr8 from macaddrs;
          mac           
-------------------------
 08:00:2b:ff:fe:01:02:03
(1 row)

See also MACADDR8 and Network Address Functions.

Parent topic:SQL Data Types