小编DRV*_*DRV的帖子

如何将json数据插入postgres数据库表

我是初学者,尝试使用教程将 JSON 值插入数据库

我使用以下命令创建了该表

CREATE TABLE table_name( id character varying(50),
                         data json NOT NULL,
                         active boolean NOT NULL,
                         created_at timestamp with time zone NOT NULL,
                         updated_at timestamp with time zone NOT NULL,
                         CONSTRAINT table_name_pkey PRIMARY KEY (id)
                       );
Run Code Online (Sandbox Code Playgroud)

该表是使用 table_name 创建的。

现在我尝试将值插入数据库:

INSERT INTO table_name
SELECT id,data,active,created_at,updated_at
FROM json_populate_record (NULL::table_name,
     '{
        "id": "1",
        "data":{
                "key":"value"
                },
         "active":true,
         "created_at": SELECT NOW(),
         "updated_at": SELECT NOW()
       }'
  );
Run Code Online (Sandbox Code Playgroud)

它抛出以下错误

错误:类型 JSON '{ 的输入语法无效

任何人都可以帮助我解析 JSON 值并将其插入数据库吗?

postgresql json sql-insert

9
推荐指数
1
解决办法
5万
查看次数

标签 统计

json ×1

postgresql ×1

sql-insert ×1