用于IPv4地址的PostgreSQL字段数据类型 [英] PostgreSQL field data type for IPv4 addresses
问题描述
PostgreSQL中IPv4地址的正确数据类型是什么?
What is the correct data type for IPv4 addresses in PostgreSQL?
我听说过 inet
,但从来没有使用它。
I heard about inet
, but never used it.
我需要执行 SELECT
查询,例如 SELECT ... WHERE ip = '99 .88.99.88'
它应该支持人类可读格式的输出(人类可读,我的意思是'99 .88.99.88')。
I need to perform SELECT
queries like SELECT ... WHERE ip = '99.88.99.88'
and it should support the output of human readable format (by human readable I mean '99.88.99.88').
能够 SELECT
那个子网的IP地址会很高兴。
It would be nice to have the ability to SELECT
the IP addresses by there subnet.
感谢任何建议提前!
推荐答案
内置的 cidr
和 inet
类型将执行您想要的操作并提供合适的运营商:
The built-in cidr
and inet
types will do what you want and provide suitable operators:
regress=> SELECT '192.168.1.19'::inet << '192.168.1.0/24'::cidr;
?column?
----------
t
(1 row)
请参阅有关网络数据类型操作符和函数的PostgreSQL文档以及网络数据类型。
See the PostgreSQL documentation on network datatype operators and functions and on the network datatypes.
为 cidr
和 inet
类型提供了有限的索引支持;特别是,范围内地址类型的查询至少在地址为常量的情况下变为范围查询。请参阅此(相当旧的)主题。
Limited index support is provided for the cidr
and inet
types; in particular, 'address in range' type queries are turned into range queries at least where the address is a constant. See this (rather old) thread.
另请参阅Chris的观点 ip4r
。
See also Chris's point re ip4r
.
这篇关于用于IPv4地址的PostgreSQL字段数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!