PG::UndefinedObject: ERROR: type "hstore" does not exist,但实际上它是存在的。

11

首先,这可能看起来像是重复的:

postgres hstore 同时存在和不存在

但实际上不是。虽然我在这种情况下收到了相同的错误信息。当检查数据库是否安装了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

同时它也在模板_1数据库中。

因此,当我试图运行迁移以添加hstore时,出现了PG::Error: ERROR:扩展“hstore”已存在的错误提示。当我注释掉这个迁移后,在下一个需要使用hstore的迁移上,它会提示PG::UndefinedObject: ERROR:类型“hstore”不存在,这有点矛盾。

这是一个Rails 4.0.1应用程序,配有PostgreSQL 9,并且我在这台机器上运行的其他几个项目中使用了hstore。

3个回答

18

您在名为hstore的模式中安装了hstore扩展,这个模式可能不在您的默认search_path中。

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

  • 在连接设置期间将hstore添加到search_path中;
  • 使用ALTER USER ... SETALTER DATABASE ... SEThstore添加到search_path中;
  • 将hstore扩展从hstore模式移动到public;或者
  • hstore的所有引用都必须指定模式,例如hstore.hstore(...)。 运算符也必须这样做; -> 变成 OPERATOR(hstore.->)

1
将其添加到search_path是修复此问题最简单的方法。 - Alain

5
这是我解决问题的方法。 创建一个名为“extensions”的模式,并授权给您在项目中使用的数据库用户名。
psql -U postgres -d template1 -c "CREATE SCHEMA extensions AUTHORIZATION <yourDbUserName>;"

在创建的模式(extensions)中创建扩展。
psql -U postgres -d template1 -c "CREATE EXTENSION IF NOT EXISTS hstore SCHEMA extensions;"

“AUTHORIZATION” 是我缺失的部分,谢谢! - Eyal Roth

2
这是我在Ubuntu 18.04中解决此问题的方法。
  1. 提供postgres超级用户访问权限。

    sudo su postgres

  2. 然后运行:

    psql -U postgres your_database_name -c 'create extension hstore;'

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

  • 连接到您的数据库

    psql -d your_database_name -U your_user_role

alter table your_table_name add your_column_name HSTORE;

虽然可能有多种不同的方法来解决它,但我是用这种方式解决的。

希望这能帮助像我这样的初学者。


2
创建扩展名为hstore的扩展。 - Eric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接