【问题标题】:Cannot Save Grafana Datasource - not-null Postgres Error无法保存 Grafana 数据源 - 非空 Postgres 错误
【发布时间】:2018-12-27 05:48:56
【问题描述】:

我似乎不再能够在 Grafana 中保存新的数据源。

特别是我正在尝试添加新的 InfluxDB 数据库作为数据源。当点击 Add 按钮时,它会在 UI 中弹出 Problem! Failed to add datasource 的错误。

Grafana 日志显示以下内容:

t=2018-07-17T09:59:32+0000 lvl=eror msg="Failed to add datasource" logger=context userId=0 orgId=1 uname= error="pq: null value in column \"id\" violates not-null constraint"

检查数据库日志(PostgreSQL)有一个相关的错误:

2018-07-19 07:12:46 UTC:10.204.145.134(36768):admin@grafana:[477]:DETAIL: Failing row contains (null, 1, 0, influxdb, jenkins, proxy, http://localhost:8086, root, root, jenkins, f, , , f, {}, 2018-07-19 07:12:46, 2018-07-19 07:12:46, f, {}). 2018-07-19 07:12:46 UTC:10.204.145.134(36768):admin@grafana:[477]:STATEMENT: INSERT INTO "data_source"

如您所见,UI 似乎试图插入 null 作为产生错误的索引。

虽然我们最近迁移了数据库(从一个 PG 迁移到另一个相同版本),但配置并没有改变,似乎也没有任何其他不良影响。

编辑:似乎这实际上会影响 Grafana 在添加新资源时尝试执行的任何数据库操作。我刚刚让开发人员尝试导入新仪表板,而 PostgreSQL 日志显示类似的错误:

2018-07-19 08:05:07 UTC:10.204.25.220(34412):sharedadmin@grafana:[14263]:DETAIL: Failing row contains (null, 1, pcs-again, PCS-AGAIN, {"__requires":[{"id":"grafana","name":"Grafana","type":"grafana"..., 1, 2018-07-19 08:05:07, 2018-07-19 08:05:07, -1, -1, 0, ).

【问题讨论】:

    标签: postgresql null amazon-rds dashboard grafana


    【解决方案1】:

    经过大量研究,我们设法找到了答案。问题在于我们用于从一个 RDS 实例迁移到另一个实例的 AWS 数据库迁移服务 (DMS)。似乎 DMS 不能很好地处理 PostgreSQL 到 PostgreSQL,一些注意事项可以在文档 here 中找到。

    在 Grafana 的情况下,流复制没有选择列修饰符。迁移的表之一:

    grafana-> \d data_source
                        Table "public.data_source"
           Column        |              Type              | Modifiers
    ---------------------+--------------------------------+-----------
     id                  | integer                        | not null
     org_id              | bigint                         | not null
     version             | integer                        | not null
     type                | character varying(255)         | not null
     name                | character varying(190)         | not null
     access              | character varying(255)         | not null
     url                 | character varying(255)         | not null
     password            | character varying(255)         |
     user                | character varying(255)         |
     database            | character varying(255)         |
     basic_auth          | boolean                        | not null
     basic_auth_user     | character varying(255)         |
     basic_auth_password | character varying(255)         |
     is_default          | boolean                        | not null
     json_data           | text                           |
     created             | timestamp(6) without time zone | not null
     updated             | timestamp(6) without time zone | not null
     with_credentials    | boolean                        | not null
     secure_json_data    | text                           |
    Indexes:
        "data_source_pkey" PRIMARY KEY, btree (id)
    

    以及来自非迁移实例的对应表:

    grafana=> \d data_source
                                              Table "public.data_source"
           Column        |            Type             |                         Modifiers
    ---------------------+-----------------------------+-----------------------------------------------------------
     id                  | integer                     | not null default nextval('data_source_id_seq1'::regclass)
     org_id              | bigint                      | not null
     version             | integer                     | not null
     type                | character varying(255)      | not null
     name                | character varying(190)      | not null
     access              | character varying(255)      | not null
     url                 | character varying(255)      | not null
     password            | character varying(255)      |
     user                | character varying(255)      |
     database            | character varying(255)      |
     basic_auth          | boolean                     | not null
     basic_auth_user     | character varying(255)      |
     basic_auth_password | character varying(255)      |
     is_default          | boolean                     | not null
     json_data           | text                        |
     created             | timestamp without time zone | not null
     updated             | timestamp without time zone | not null
     with_credentials    | boolean                     | not null default false
     secure_json_data    | text                        |
    Indexes:
        "data_source_pkey1" PRIMARY KEY, btree (id)
        "UQE_data_source_org_id_name" UNIQUE, btree (org_id, name)
        "IDX_data_source_org_id" btree (org_id)
    

    故事的寓意是 DMS 并不适合所有数据库,请仔细阅读文档,在某些情况下使用本地 PostgreSQL 工具会更好。

    为了解决这个特殊问题,我们删除了数据库(在确保导出了所有仪表板之后),重新创建它,然后重新启动 Grafana。

    【讨论】:

      【解决方案2】:

      请检查您的 PSQL 表 data_sourceuser_auth

      grafana=# \d data_source
                                                   Table "public.data_source"
      
             Column        |            Type             | Collation | Nullable |                 Default
      ---------------------+-----------------------------+-----------+----------+-----------------------------------------
       id                  | integer                     |           | not null | nextval('data_source_id_seq'::regclass)    <=== (Probably its ok Constraint)
      
      grafana=# \d user_auth
                                                   Table "public.user_auth"
              Column        |            Type             | Collation | Nullable |                Default
      ----------------------+-----------------------------+-----------+----------+---------------------------------------
       id                   | bigint                      |           | not null |                                            <=== (Probably it is blank)
      

      grafana PSQL DB 中以grafana 用户身份登录并运行:

      Alter table user_auth:
       alter table user_auth
       alter column id TYPE integer,
       alter column id SET not null;
      
      CREATE SEQUENCE user_auth_id_seq 
       START WITH 1
       MINVALUE 1
       NO MAXVALUE
       CACHE 1;
      

      CMD:

      grafana=# CREATE SEQUENCE user_auth_id_seq
      grafana-# START WITH 1
      grafana-# MINVALUE 1
      grafana-# NO MAXVALUE
      grafana-# CACHE 1;
      CREATE SEQUENCE
      grafana=# ALTER TABLE user_auth ALTER COLUMN id SET DEFAULT nextval('user_auth_id_seq');
      ALTER TABLE
      
      grafana=# \d user_auth
                                                   Table "public.user_auth"
              Column        |            Type             | Collation | Nullable |                Default
      ----------------------+-----------------------------+-----------+----------+---------------------------------------
       id                   | integer                     |           | not null | nextval('user_auth_id_seq'::regclass)    <=== (check it is ok now)
      

      测试下限:

      grafana=# SELECT nextval('user_auth_id_seq');
       nextval
      ---------
             1
      (1 row)
      
      grafana=# SELECT nextval('user_auth_id_seq');
       nextval
      ---------
             2
      (1 row)
      
      grafana=# SELECT nextval('user_auth_id_seq');
       nextval
      ---------
             3
      (1 row)
      

      尝试连接 ldap。

      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多