当前位置: 首页 > news >正文

PostgreSQL如何支持PL/Python过程语言

瀚高数据库
目录
环境
文档用途
详细信息

环境
系统平台:Linux x86-64 Red Hat Enterprise Linux 7
版本:10.4
文档用途
本文档主要介绍PostgreSQL如何支持PL/Python过程语言,如何创建plpython扩展。

详细信息
一、PostgreSQL支持python语言的前提条件

1、本地必须安装python

python有python2和python3的版本,执行下面命令查看python版本

image.png

2、本地必须有python的动态库文件,例如libpython2.7.so.1.0、libpython3.10.so.1.0

3、编译PG源码时,./configure必须配置–with-python

./configure --prefix=/home/pg10_python/pgdb --with-python

执行该配置命令时,会check本地是否已安装python,且是否存在必要的python库文件,同时还会选择python版本。

python版本的选择是根据/bin或者/usr/bin目录下的python命令指向的版本决定的。

例如:

1)python和python-config指向的是python2的版本

image.png

PG源码中执行./configure时,使用的是python2

[pg10_python@localhost postgresql-10.21]$ ./configure --prefix=/home/pg10_python/pgdb --with-python......checking for python... /bin/pythonconfigure: using python 2.7.5 (default, Jun 28 2022, 15:30:04)......

2)python和python-config指向的是python3的版本

image.png

PG源码中执行./configure时,使用的是python3

[pg10_python@localhost postgresql-10.21]$ ./configure --prefix=/home/pg10_python/pgdb --with-python......checking for python... /bin/pythonconfigure: using python 3.10.5 (main, Jul 21 2022, 16:11:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]......

二、PG源码中包含python模块的源码,在/src/pl/plpython目录下,该目录下包含plpython2u和plpython3u两个版本,plpythonu默认使用python2的版本。

image.png

plpython2u对应python2,plpython3u对应python3

三、以python3为例,编译PG源码实现对python的支持

1、编译PG源码

[pg10_python@localhost ~]$ cd tmp/postgresql-10.21/[pg10_python@localhost postgresql-10.21]$ ./configure --prefix=/home/pg10_python/pgdb --with-python[pg10_python@localhost postgresql-10.21]$ make[pg10_python@localhost postgresql-10.21]$ make install

2、PG安装完成后,查看生成的python3的相关文件

image.png

3、初始化data目录,然后在数据库中创建python扩展

##连接数据库[pg10_python@localhost bin]$ ./psql -U postgres -d postgres -p 5432##创建plpython扩展postgres=# create extension plpython3u;CREATE EXTENSIONpostgres=# \dx plpython3uList of installed extensionsName    | Version |   Schema   |                Description------------+---------+------------+-------------------------------------------plpython3u | 1.0     | pg_catalog | PL/Python3U untrusted procedural language(1 row)##创建plpython3u语言的函数postgres=# CREATE OR REPLACE FUNCTION pyclean(arg text)RETURNS textAS $$global argimport rearg=str(arg)arg=arg.strip(' ,')#去掉首尾空格if arg == '' or arg == 'None':arg=Nonereturn arg$$ LANGUAGE plpython3u;CREATE FUNCTION##测试python函数postgres=# select length(pyclean('abc d e f  '));length--------9(1 row)

四、正在运行的PG库中如何创建python扩展

一个场景是安装PG库时,并没有配置–with-python,导致PG库不能编写python函数。

那么在不重新安装且不重启数据库的前提下如何支持python,有以下步骤:

1、执行当前数据库的bin目录下的pg_config命令,用于查看CONFIGURE的配置内容

[pg10_python@localhost bin]$ ./pg_config......CONFIGURE = '--prefix=/home/pg10_python/pgdb_nopython'......

2、跳转到PG源码目录,加上–with-python重新配置一下

注:只做配置,不执行make和make install

[pg10_python@localhost ~]$ cd tmp/postgresql-10.21/[pg10_python@localhost postgresql-10.21]$ ./configure --prefix=/home/pg10_python/pgdb_nopython --with-python

3、配置完成后,单独编译安装PG源码中的plpython源码

[pg10_python@localhost postgresql-10.21]$ cd src/pl/plpython/[pg10_python@localhost plpython]$ make[pg10_python@localhost plpython]$ make install

4、编译安装后,在数据库中就可以查到该扩展

postgres=# select * from pg_available_extensions where name like '%plpython%';name    | default_version | installed_version |                  comment------------+-----------------+-------------------+-------------------------------------------plpython3u | 1.0             | 1.0               | PL/Python3U untrusted procedural language(1 row)

5、创建plpython3u扩展,编写函数测试即可。

注:必须加上–with-python重新配置一下,否则直接编译plpython会失败

五、查看PG的lib目录下编译生成的plpython库文件的依赖

plpython3.so依赖python3版本的libpython3.10.so.1.0库文件

image.png

http://www.lryc.cn/news/172644.html

相关文章:

  • 【C++】STL之适配器---用deque实现栈和队列
  • PHY6230低成本遥控灯控芯片国产蓝牙BLE5.2 2.4G SoC
  • OceanBase杨传辉传递亚运火炬:国产数据库为“智能亚运”提供稳稳支持
  • 分布式锁实现方法
  • 软件测试缺陷报告详解
  • pytorch冻结参数训练的坑
  • P1827 [USACO3.4] 美国血统 American Heritage(前序 + 中序 生成后序)
  • 【四、centOS安装docker】
  • 想学嵌入式开发,薪资怎么样?
  • SQL死锁进程内容查询语句
  • Ubuntu 20.04中Nightingale二进制部署
  • 深入探讨Java面试中内存泄漏:如何识别、预防和解决
  • win10 安装.net framework 3.5,错误代码0x8024401C
  • 杂记 | Langchain中few-shot提示词模板的使用(给提示词添加示例)
  • SVN -基础
  • MySQL基础终端命令与Python简单操作MySQL
  • 编译原理.龙书学习1
  • anaconda安装完成之后输入conda -V没有反应
  • netty报文解析之粘包半包问题
  • EasyCode整合mybatis-plus的配置
  • 实施预测性维护解决方案的挑战及PreMaint的应对方法
  • 1. js中let、var、const定义变量区别与方式
  • 【STM32学习】I2C通信协议 | OLED屏
  • Nvme Spec 第一章节学习
  • 第一章:最新版零基础学习 PYTHON 教程(第九节 - Python 语句中的 – 多行语句)
  • kafka 3.0 离线安装
  • MySQL数据库入门到精通2--基础篇(函数,约束,多表查询,事务)
  • c-数据在内存中的存储-day7
  • 3D大模型如何轻量化?试试HOOPS Communicator,轻松读取10G超大模型!
  • go并发操作且限制数量