如何使非超级用户可以使用 Postgres 扩展
How do I make Postgres extension available to non superuser
我安装了一个 Postgres 扩展(unaccent)
sudo su posgres
psql create extension unaccent
现在我可以在 sql、 中使用 unacccent,但前提是我是 Postgres 用户。
如何让 all/another 用户可以使用 Postgres 扩展
(我正在 Ubuntu 使用通过 apt-install 安装的 Postgres 9.3.5)
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$psqljthinksearch
psql (9.3.5)
键入 "help" 寻求帮助。
我给了用户超级用户角色,但这没有帮助,然后按照建议将模式名称放入 ,这对错误消息有影响但仍然没有用
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=#
基于此错误消息:
ERROR: text search dictionary "unaccent" does not exist
和上一个没有模式前缀的 unaccent
没有找到,这意味着 unaccent 函数所在的 public
模式不在你的 search_path
中。
碰巧 unaccent
在这种情况下失败了,因为它是一个字典函数,基本上它需要通过 search_path
.
找到它的东西
这在 Does PostgreSQL support “accent insensitive” collations?
中有更详细的解释
一旦 public
模式被添加到需要调用它的用户的 search_path
中(这通常是默认设置),这应该可以工作并且他们不需要是超级用户.
或者,如果此解决方案不可接受,您也可以使用嵌入模式并添加不变性的中间存根函数,如上面链接的答案中所建议的那样。
这是我的解决方案。我有一个超级用户 (postgres) 和一个非超级用户 (pg4e_user_8087f) 和一个数据库 (pg4e),我想安装 hstore
和 uuid-ossp
扩展并在不成为超级用户的情况下使用它们。我正在使用 PostgreSQL 11。
超级用户 window:
\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;
非超级用户window完成上述命令后:
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)
有一次,我已经想通了几乎所有的问题,但没有意识到超级用户 必须连接 到有问题的数据库才能创建然后允许扩展.一旦我发现这是针对特定数据库完成的,它很快就会到位。
我安装了一个 Postgres 扩展(unaccent)
sudo su posgres
psql create extension unaccent
现在我可以在 sql、 中使用 unacccent,但前提是我是 Postgres 用户。
如何让 all/another 用户可以使用 Postgres 扩展
(我正在 Ubuntu 使用通过 apt-install 安装的 Postgres 9.3.5)
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$psqljthinksearch psql (9.3.5) 键入 "help" 寻求帮助。
我给了用户超级用户角色,但这没有帮助,然后按照建议将模式名称放入 ,这对错误消息有影响但仍然没有用
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=#
基于此错误消息:
ERROR: text search dictionary "unaccent" does not exist
和上一个没有模式前缀的 unaccent
没有找到,这意味着 unaccent 函数所在的 public
模式不在你的 search_path
中。
碰巧 unaccent
在这种情况下失败了,因为它是一个字典函数,基本上它需要通过 search_path
.
这在 Does PostgreSQL support “accent insensitive” collations?
中有更详细的解释一旦 public
模式被添加到需要调用它的用户的 search_path
中(这通常是默认设置),这应该可以工作并且他们不需要是超级用户.
或者,如果此解决方案不可接受,您也可以使用嵌入模式并添加不变性的中间存根函数,如上面链接的答案中所建议的那样。
这是我的解决方案。我有一个超级用户 (postgres) 和一个非超级用户 (pg4e_user_8087f) 和一个数据库 (pg4e),我想安装 hstore
和 uuid-ossp
扩展并在不成为超级用户的情况下使用它们。我正在使用 PostgreSQL 11。
超级用户 window:
\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;
非超级用户window完成上述命令后:
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)
有一次,我已经想通了几乎所有的问题,但没有意识到超级用户 必须连接 到有问题的数据库才能创建然后允许扩展.一旦我发现这是针对特定数据库完成的,它很快就会到位。