INET

The INET data type stores an IPv4 or IPv6 address.

Syntax

A constant value of type INET can be expressed using aninterpreted literal, or astring literalannotated withtype INET orcoerced to typeINET.

INET constants can be expressed using the following formats:

FormatDescription
IPv4Standard RFC791-specified format of 4 octets expressed individually in decimal numbers and separated by periods. Optionally, the address can be followed by a subnet mask.Examples: '190.0.0.0', '190.0.0.0/24'
IPv6Standard RFC8200-specified format of 8 colon-separated groups of 4 hexadecimal digits. An IPv6 address can be mapped to an IPv4 address. Optionally, the address can be followed by a subnet mask.Examples: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1', '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120', '::ffff:192.168.0.1/24'

Note:
IPv4 addresses will sort before IPv6 addresses, including IPv4-mapped IPv6 addresses.

Size

An INET value is 32 bits for IPv4 or 128 bits for IPv6.

Example

  1. > CREATE TABLE computers (
  2. ip INET PRIMARY KEY,
  3. user_email STRING,
  4. registration_date DATE
  5. );
  1. > SHOW COLUMNS FROM computers;
  1. +-------------------+-----------+-------------+----------------+-----------------------+-------------+
  2. | column_name | data_type | is_nullable | column_default | generation_expression | indices |
  3. +-------------------+-----------+-------------+----------------+-----------------------+-------------+
  4. | ip | INET | false | NULL | | {"primary"} |
  5. | user_email | STRING | true | NULL | | {} |
  6. | registration_date | DATE | true | NULL | | {} |
  7. +-------------------+-----------+-------------+----------------+-----------------------+-------------+
  8. (3 rows)
  1. > INSERT INTO computers
  2. VALUES
  3. ('192.168.0.1', 'info@cockroachlabs.com', '2018-01-31'),
  4. ('192.168.0.2/10', 'lauren@cockroachlabs.com', '2018-01-31'),
  5. ('2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120', 'test@cockroachlabs.com', '2018-01-31');
  1. > SELECT * FROM computers;
  1. +--------------------------------------+--------------------------+---------------------------+
  2. | ip | user_email | registration_date |
  3. +--------------------------------------+--------------------------+---------------------------+
  4. | 192.168.0.1 | info@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
  5. | 192.168.0.2/10 | lauren@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
  6. | 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120 | test@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
  7. +--------------------------------------+--------------------------+---------------------------+

Supported casting and conversion

INET values can be cast to the following data type:

  • STRING - Converts to format 'Address/subnet'.

See also

Was this page helpful?
YesNo