【问题标题】:PG::UndefinedObject: ERROR: type "hstore" does not exist but it doesPG::UndefinedObject:错误:类型“hstore”不存在,但确实存在
【发布时间】:2014-03-21 11:47:15
【问题描述】:

首先,这可能看起来像:

postgres hstore exists and doesn't exist at same time

但事实并非如此。虽然我在这种情况下收到相同的错误消息。在查看数据库上是否安装了 hstore 时,我们可以看到它是:

./psql -d photographerio_development -c '\dx'
                       List of installed extensions
  Name   | Version |   Schema   |                   Description                    
---------+---------+------------+--------------------------------------------------
 hstore  | 1.2     | hstore     | data type for storing sets of (key, value) pairs
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language

它也在 template_1 DB 上。

所以,当我尝试运行迁移以添加 hstore 时,我得到了 PG::Error: ERROR: extension "hstore" already exists,当我注释掉这个迁移时,在下一个需要 hstore 的迁移中,它显示 PG::UndefinedObject: ERROR: type "hstore" does not exist 这有点一个悖论。

这是一个带有 postgresql 9 的 Rails 4.0.1 应用程序,我让 hstore 在这台机器上运行其他几个项目。

【问题讨论】:

    标签: ruby-on-rails postgresql hstore


    【解决方案1】:

    您已在名为 hstore 的架构中安装了 hstore 扩展,这可能不在您的默认 search_path 上。

    您必须执行以下操作之一:

    • 在连接设置期间将hstore 添加到search_path
    • 使用ALTER USER ... SETALTER DATABASE ... SEThstore 添加到search_path
    • 将 hstore 扩展从 hstore 架构移动到 public;或
    • 模式限定所有对hstore 的引用,例如hstore.hstore(...)。操作员也必须这样做; -> 变为 OPERATOR(hstore.->)

    【讨论】:

    • 将其添加到 search_path 是解决此问题的最简单方法。
    【解决方案2】:

    这就是我解决问题的方法。 创建一个名为 extensions 的模式,并授予您在项目中使用的 db 用户名的权限。

    psql -U postgres -d template1 -c "CREATE SCHEMA extensions AUTHORIZATION <yourDbUserName>;"
    

    在创建的架构中创建扩展(扩展)

    psql -U postgres -d template1 -c "CREATE EXTENSION IF NOT EXISTS hstore SCHEMA extensions;"
    

    【讨论】:

    • AUTHORIZATION 是我缺少的部分,谢谢!
    【解决方案3】:

    这就是我在 ubuntu 18.04 中解决此问题的方法。

    1. 提供 postgres 超级用户访问权限。

      sudo su postgres

    2. 然后我运行:

      psql -U postgres your_database_name -c '创建扩展 hstore;'

    现在我可以更改表 your_database_name 并在其中添加 hstore 类型列。

    • 连接到您的数据库

      psql -d your_database_name -U your_user_role

    alter table your_table_name add your_column_name HSTORE;
    

    虽然可能有很多不同的方法可以做到这一点,但我以这种方式解决它。

    希望这对像我这样的新手用户有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2023-03-03
      • 2014-04-02
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多