【问题标题】:How do I make Postgres extension available to non superuser如何使非超级用户可以使用 Postgres 扩展
【发布时间】:2015-08-28 07:45:01
【问题描述】:

我用

安装了一个 Postgres 扩展(非重音)
sudo su posgres
psql create extension unaccent

现在我可以在 sql 中使用 unccent,但前提是我是 Postgres 用户

如何让所有/其他用户都可以使用 Postgres 扩展

(我在使用 apt-install 安装的 Postgres 9.3.5 的 Ubuntu 上)

jthinksearch=# \dx;
                         List of installed extensions
   Name   | Version |   Schema   |                 Description
----------+---------+------------+---------------------------------------------
 plpgsql  | 1.0     | pg_catalog | PL/pgSQL procedural language
 unaccent | 1.0     | public     | text search dictionary that removes accents
(2 rows)

jthinksearch=#


jthinksearch=> \du;
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 ubuntu    |                                                | {}

postgres@ip-172-31-39-147:/home/ubuntu/code/jthinksearch/reports/src/main/sql$ 退出 ubuntu@ip-172-31-39-147:~/code/jthinksearch/reports/src/main/sql$ psql jthinksearch psql (9.3.5) 输入“帮助”以获得帮助。

我给了用户超级用户角色,但没有帮助,然后按照建议将架构名称放入,这对错误消息有影响但仍然不起作用

jthinksearch=# \du;
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 ubuntu    | Superuser                                      | {}

jthinksearch=# select unaccent(name) from musicbrainz.artist where id=195660;
ERROR:  function unaccent(character varying) does not exist
LINE 1: select unaccent(name) from musicbrainz.artist where id=19566...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
jthinksearch=# ^C
jthinksearch=# select public.unaccent(name) from musicbrainz.artist where id=195660;
ERROR:  text search dictionary "unaccent" does not exist
jthinksearch=#

【问题讨论】:

  • grant 访问扩展中的功能。如果您在自己的架构中创建扩展,您可以简单地授予对该架构中所有内容的访问权限
  • 如何,拜托,我尝试了“将 SCHEMA public 中的所有功能全部授予 ubuntu”,但没有效果
  • @a_horse_with_no_name 尝试了我所有的模式,但仍然不起作用
  • 对不起,你说only if I am the postgres用户然后你是光栅ubuntu用户?
  • @FabrizioMazzoni 是的,这就是我想要做的,ubuntu 是我使用的默认用户,但我必须 su 到 postgres 用户才能安装,但我仍然不能将它用作 ubuntu 用户

标签: postgresql psql


【解决方案1】:

基于此错误消息:

错误:文本搜索字典“unaccent”不存在

和上一个没有找到模式前缀的unaccent,这意味着unaccent函数所在的public模式不在你的search_path中。

在这种情况下unaccent 会失败,因为它是一个字典函数,基本上它需要通过search_path 找到它的内容。

这在Does PostgreSQL support “accent insensitive” collations?中有更详细的解释

一旦将public 架构添加到需要调用它的用户的search_path(这通常是默认设置),这应该可以工作,并且他们不需要是超级用户。

或者,如果此解决方案不可接受,您也可以使用嵌入架构并添加不变性的中间存根函数,如上面链接的答案中所建议的那样。

【讨论】:

  • 啊,谢谢。是的,这就是我的数据库包含两个我需要使用的模式的问题,所以我之前已经完成了 ALTER USER ubuntu SET search_path = discogs,musicbrainz;更改为 ALTER USER ubuntu SET search_path = discogs,musicbrainz, public;修复了问题
  • 我有完全相同的问题,如果没有非 postgres 用户,则无法使用 unaccent 扩展名。但仍然无法正常工作。
【解决方案2】:

这是我的解决方案。我有一个超级用户 (postgres) 和一个非超级用户 (pg4e_user_8087f) 和一个数据库 (pg4e),我想安装 hstoreuuid-ossp 扩展并在不成为超级用户的情况下使用它们。我正在使用 PostgreSQL 11。

超级用户窗口:

\c pg4e
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
ALTER EXTENSION "uuid-ossp" SET SCHEMA public;
CREATE EXTENSION IF NOT EXISTS "hstore";
ALTER EXTENSION "hstore" SET SCHEMA public;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO pg4e_user_8087f; 

以上命令完成后的非超级用户窗口:

pg4e=> \dx
                            List of installed extensions
   Name    | Version |   Schema   |                   Description                    
-----------+---------+------------+--------------------------------------------------
 hstore    | 1.5     | public     | data type for storing sets of (key, value) pairs
 plpgsql   | 1.0     | pg_catalog | PL/pgSQL procedural language
 uuid-ossp | 1.1     | public     | generate universally unique identifiers (UUIDs)

pg4e=> select uuid_generate_v1();
           uuid_generate_v1           
--------------------------------------
 2114df5a-16bb-11ea-8000-468ce7a721ef
(1 row)

pg4e=> SELECT 'a=>1,b=>2'::hstore;
       hstore       
--------------------
 "a"=>"1", "b"=>"2"
(1 row)

在某一时刻,我几乎想通了所有这些,但没有意识到超级用户必须连接到有问题的数据库才能创建并允许扩展。一旦我发现这是针对特定数据库完成的,它很快就到位了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2020-06-15
    • 2012-04-15
    • 2022-11-25
    • 1970-01-01
    • 2020-01-03
    • 2012-06-01
    相关资源
    最近更新 更多