代码之家  ›  专栏  ›  技术社区  ›  Adam Matan

PostgreSQL:作为普通用户运行python存储过程

  •  7
  • Adam Matan  · 技术社区  · 15 年前

    我已经在PostgreSQL服务器上安装了pl/python postgres 特权:

    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 回复  |  直到 12 年前
        1
  •  9
  •   claymation    13 年前
    UPDATE pg_language SET lanpltrusted = true WHERE lanname = 'plpythonu';
    
        2
  •  1
  •   loginx    15 年前

    不幸的是,我不相信可以运行不受信任的解释程序,除非您的Postgres帐户具有超级用户访问权限。如果您是数据库服务器管理员,CreateUser将询问您新帐户是否应为超级用户。

    “untrusted”标志并不意味着运行时不稳定或不可靠,只是它的安全模型不适合存储过程解释器。这可能会导致存储过程中的权限提升,或潜在的灾难性安全错误。

    如果您不能以Postgres用户的身份运行或创建超级用户帐户,恐怕您将不得不跳过pl/python,并建议您签出pl/pgsql。 http://www.postgresql.org/docs/8.3/interactive/plpgsql.html

        3
  •  1
  •   Milen A. Radev    15 年前

    对语言授予[用法]意味着相关用户可以使用该语言创建函数。创建后,必须使用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=>