PostgreSQL:以普通用户身份运行Python存储过程
我在我的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 个回答
在这里,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=>
很遗憾,我认为如果你的Postgres账户没有超级用户权限,是无法运行不受信任的解释器的。如果你是数据库服务器的管理员,使用createuser命令时会询问你新账户是否应该是超级用户。
这里的“untrusted”标志并不是说运行环境不稳定或不可靠,而是说它的安全模型不太适合用作存储过程解释器。这可能会导致存储过程的权限提升,或者可能出现严重的安全漏洞。
如果你无法以postgres用户身份运行或者创建超级用户账户,那我恐怕你得跳过pl/python,建议你看看pl/pgsql。http://www.postgresql.org/docs/8.3/interactive/plpgsql.html
在编程中,有时候我们需要处理一些数据,这些数据可能会有很多不同的格式。比如说,我们可能会遇到一些文本文件,里面的内容并不是我们想要的样子。这时候,我们就需要用一些工具来把这些数据整理得更好,让它们变得更容易使用。
有些时候,数据可能会包含一些我们不需要的信息,或者格式不太对,这就像是一个杂乱的房间,我们需要把它整理得干干净净。为了做到这一点,我们可以使用一些编程语言提供的功能,来帮助我们提取出我们真正需要的部分。
总之,处理数据就像是清理房间一样,我们需要找到合适的方法,把有用的东西留下,把多余的东西扔掉,这样才能让我们的工作变得更加高效。
UPDATE pg_language SET lanpltrusted = true WHERE lanname = 'plpythonu';