PostgreSQL:以普通用户身份运行Python存储过程

时间:2010-05-17 11:46:37

标签: python postgresql

我在postgres权限下的postgresql服务器上安装了PL / Python:

netherlands=# CREATE PROCEDURAL LANGUAGE plpythonu;
CREATE LANGUAGE      

现在我需要授予权限,以便我可以将其用作普通用户:

netherlands=# GRANT ALL ON LANGUAGE plpythonu TO adam;
ERROR:  language "plpythonu" is not trusted
HINT:  Only superusers can use untrusted languages.

我知道python不是一种“可信赖的”语言,但我愿意把握机会。有没有办法说服PostgreSQL让我以普通用户身份运行Python存储过程?

3 个答案:

答案 0 :(得分:9)

UPDATE pg_language SET lanpltrusted = true WHERE lanname = 'plpythonu';

答案 1 :(得分:1)

不幸的是,除非您的postgres帐户具有超级用户访问权限,否则我认为不可能运行不受信任的解释器。如果您是数据库服务器管理员,createuser将询问您新帐户是否应该是超级用户。

'untrusted'标志并不意味着运行时不稳定或不可靠,只是因为它的安全模型不适合作为存储过程解释器。这可能会导致存储过程的权限升级,或者可能导致灾难性的安全漏洞。

如果您无法以postgres用户身份运行或创建超级用户帐户,我恐怕您将不得不跳过pl / python,并建议您查看pl / pgsql。 http://www.postgresql.org/docs/8.3/interactive/plpgsql.html

答案 2 :(得分:1)

语言上的GRANT [USAGE]表示相关用户可以使用该语言创建功能。创建后,您必须使用GRANT EXECUTE以允许其他用户使用它们。

postgres@dev:~$ psql
Welcome to psql 8.3.9, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

postgres=# \c plpythonu_test
You are now connected to database "plpythonu_test".
plpythonu_test=# create language plpythonu;
CREATE LANGUAGE
plpythonu_test=# CREATE FUNCTION pymax (a integer, b integer)
plpythonu_test-#   RETURNS integer
plpythonu_test-# AS $$
plpythonu_test$#   if a > b:
plpythonu_test$#     return a
plpythonu_test$#   return b
plpythonu_test$# $$ LANGUAGE plpythonu;
CREATE FUNCTION
plpythonu_test=# grant execute on function pymax (a integer, b integer) to plpythonu_test;
GRANT
plpythonu_test=#



C:\Users\milen>psql.exe -U plpythonu_test -h ...
Password for user plpythonu_test:
psql (8.4.4, server 8.3.9)
WARNING: psql version 8.4, server version 8.3.
         Some psql features might not work.
WARNING: Console code page (866) differs from Windows code page (1251)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

plpythonu_test=> select pymax(1,2);
 pymax
-------
     2
(1 row)


plpythonu_test=>