host() 是一个系统函数,返回 IP 地址,如果存在则去除网络掩码。
host() 函数已在 PostgreSQL 6.4 中添加。
示例
host() 的基本用法示例
postgres=# SELECT host('192.168.1.2/32');
host
-------------
192.168.1.2
(1 row)
如果提供的地址不包含网络掩码,则按原样返回
postgres=# SELECT host('192.168.1.2');
host
-------------
192.168.1.2
(1 row)
如果提供了无效地址,则会引发 ERROR 错误
postgres=# SELECT host('192.168.1.2/99');
ERROR: invalid input syntax for type inet: "192.168.1.2/99"
LINE 1: SELECT host('192.168.1.2/99');
参考资料
- PostgreSQL 文档: IP 地址函数
