我对postgresql非常陌生。
我想把以下json对象存储到postgresql数据库中。
{
"host": "xxx.xxx.xx.xx"
"type": "OS"
}
请问在postgresql中应该使用什么数据类型?谢谢。
我对postgresql非常陌生。
我想把以下json对象存储到postgresql数据库中。
{
"host": "xxx.xxx.xx.xx"
"type": "OS"
}
请问在postgresql中应该使用什么数据类型?谢谢。
host
和 type
列的表中。
如果你的数据总是包含这种简单的结构,我认为没有必要将它们存储为JSON格式。你应该考虑将它们简单地存储在一个带有host
和type
列的表中。
INSERT INTO table(my_host_column, my_type_column) VALUES
(my_json ->> 'host', my_json ->> 'type');
这可使许多事情变得更加简单(搜索、更新等)。在您的情况下,Postgres为IP地址列提供了inet
类型。这样一个列可以对您的host
进行合理性检查,例如(https://www.postgresql.org/docs/current/static/datatype-net-types.html)
您可以随时使用 json_build_object('host', my_host_column, 'type', my_type_column)
重新创建JSON (https://www.postgresql.org/docs/current/static/functions-json.html)
如果您不想做任何操作,请将其存储为text
类型(我绝对不建议这样做,因为您不知道未来会带来什么)。如果要使用Postgres的JSON函数,则应将其存储为json
或jsonb
类型(https://www.postgresql.org/docs/current/static/datatype-json.html)。
jsonb
主要具有节省空间的开销(更多元数据),但在操作方面通常更快。
进一步阅读: