Updated on 2025-10-23 GMT+08:00

Network Address Functions

INET_ATON

INET_ATON(TEXT str)

Description: Specifies an IPv4 network address in string format. The function returns an address value in network byte order. If the input parameter is not a valid address, NULL is returned.

Return type: BIGINT UNSIGNED

Examples:

m_db=# SELECT INET_ATON('127'),INET_ATON('127.1'),INET_ATON('127.0.1'),INET_ATON('127.0.0.1');
 INET_ATON('127') | INET_ATON('127.1') | INET_ATON('127.0.1') | INET_ATON('127.0.0.1') 
------------------+--------------------+----------------------+------------------------
              127 |         2130706433 |           2130706433 |             2130706433
(1 row)

INET_NTOA

INET_NTOA (INT expr)

Description: Specifies a numeric IPv4 network address in network byte order. The function returns the network address in string format. If the input parameter is not a valid address, NULL is returned.

Return type: VARCHAR

Examples:

m_db=# SELECT INET_NTOA(4294902271),INET_NTOA(4294967295),INET_NTOA(511);
 INET_NTOA(4294902271) | INET_NTOA(4294967295) | INET_NTOA(511) 
-----------------------+-----------------------+----------------
 255.255.1.255         | 255.255.255.255       | 0.0.1.255
(1 row)

INET6_ATON

INET6_ATON(TEXT str)

Description: Specifies an IPv4 network address in string format. The function returns a network address in binary string format. If the input parameter is not a valid address, NULL is returned.

Return type: VARBINARY

Examples:

m_db=# SELECT HEX(INET6_ATON('1.2.3.4'));
 HEX(INET6_ATON('1.2.3.4')) 
----------------------------
 01020304
(1 row)

INET6_NTOA

INET6_NTOA(VARBINARY expr)

Description: Specifies an IPv4 network address in binary string format. The function returns an address value in network byte order. If the input parameter is a valid address of the VARBINARY or BINARY type, the function returns a valid result. Otherwise, the function returns NULL.

Return type: VARCHAR

Examples:

m_db=# SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'));
 INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8')) 
-------------------------------------------
 1:2:3:4:5:6:7:8
(1 row)

IS_IPV6

IS_IPV6(ANY expr)

Description: Specifies whether the input string is a valid IPv6 address. If it is valid, 1 is returned. If it is invalid, 0 is returned. If the input string is NULL, 0 is returned.

Return type: INT

Examples:

m_db=# SELECT IS_IPV6('1:2:3:4:5:6:7:8');
 is_ipv6 
---------
       1
(1 row)

m_db=# SELECT IS_IPV6('1');
 is_ipv6 
---------
       0
(1 row)

IS_IPV4

IS_IPV4(ANY expr)

Description: Specifies whether the input string is a valid IPv4 address. If it is valid, 1 is returned. If it is invalid, 0 is returned. If the input string is NULL, 0 is returned.

Return type: INT

Examples:

m_db=# SELECT IS_IPV4('1.2.3.4');
 is_ipv4 
---------
       1
(1 row)

m_db=# SELECT IS_IPV4('1');
 is_ipv4 
---------
       0
(1 row)