为宝塔面板预装 PostgreSQL 安装 pgcrypto 扩展

  • ~6.22K 字
  • 次阅读
  • 条评论
  1. 1. 起因
  2. 2. 折腾小记
  3. 3. 安装流程
    1. 3.1. 编译
    2. 3.2. 安装
  4. 4. 后记

这篇文章是踩坑的过程记录,其中包含着一些不可用的命令,请留意文中的提示~ 正确的编译和安装流程从 安装流程 开始

起因

博主的 PostgreSQL 是通过宝塔面板的 PostgreSQL 管理器快速安装的,版本如下:

PostgreSQL 管理器

但在一次使用 prisma 初始化数据库时发生了这样的报错:

1
2
3
ERROR: extension "pgcrypto" is not available
DETAIL: Could not open extension control file "/www/server/pgsql/share/extension/pgcrypto.control": No such file or directory.
HINT: The extension must first be installed on the system where PostgreSQL is running.

报错原因很显然,数据库找不到 /www/server/pgsql/share/extension/pgcrypto.control 这个控制文件。也就是说,宝塔面板预构建的 PG 并没有包含 pgcrypto 之类的扩展,而宝塔官方也没有提供 PostgreSQL 扩展的安装途径。

pgcrypto 是 PostgreSQL 的官方加密扩展,很多程序在涉及密码、UUID 等高级功能时会依赖它

博主的想法是不完整安装,不重新安装 PostgreSQL 数据库,直接编译 pgcrypto 扩展至宝塔 PG 目录下。这样可以防止系统中存在多个数据库造成冲突,影响数据库运行,同时也防止环境不同导致库文件动态链接失败。

于是,打开搜索引擎找啊找…

搜索结果

后续博主又以“宝塔安装pgcrypto”等关键词查询,但只找到了通过 apt 全新安装一次 PG 及扩展,再将需要的扩展复制至宝塔 PG 目录下这种方法。这并不是博主想要的,于是,又可以多一篇首发原创水文了(笑

折腾小记

以下流程不全部正确,但看看似乎也不错~ 正确的编译和安装流程从后文 安装流程 开始

博主使用的是 PG 16.1 ,为了防止最后将没用的文件丢的到处都是,博主选择了在 ~ (Home) 目录下编译

没有源码还怎么编译呀,所以先搞到对应版本的源码

1
2
3
wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.gz
tar -zxvf postgresql-16.1.tar.gz
cd postgresql-16.1

因为需要为宝塔的 PG 编译,所以自然要使用宝塔的 pg_config 来配置编译环境

1
./configure --with-pgconfig=/www/server/pgsql/bin/pg_config

配置完成后进入 pgcrypto 扩展的路径开始编译

1
2
3
cd contrib/pgcrypto
make
make install

然后试了试好像有点效果(?之后就立刻把 ~/postgresql-16.1/ 扬了,又不能用了

于是按刚刚的步骤再试一次,看看装到哪里了

1
2
3
4
5
6
7
ubuntu@ubuntu:~$ find / -name "pgcrypto.so" 2>/dev/null
/usr/local/pgsql/lib/pgcrypto.so
/home/ubuntu/postgresql-16.1/contrib/pgcrypto/pgcrypto.so
ubuntu@ubuntu:~$ find / -name "pgcrypto.control" 2>/dev/null
/usr/local/pgsql/share/extension/pgcrypto.control
/usr/local/pgsql/contrib/pgcrypto/pgcrypto.control
/home/ubuntu/postgresql-16.1/contrib/pgcrypto/pgcrypto.control

欸欸欸我的 pgcrypto,你怎么在 /usr/local/pgsql/ 里啊

那就将错就错吧,复制过去看看

1
2
3
cp /usr/local/pgsql/lib/pgcrypto.so /www/server/pgsql/lib/
cp /usr/local/pgsql/share/extension/pgcrypto.control /www/server/pgsql/share/extension/
find /usr/local/pgsql/share/extension/ -name "pgcrypto--*.sql" -exec cp {} /www/server/pgsql/share/extension/ \;
1
2
3
4
5
6
7
8
ubuntu@ubuntu:~$ ls /www/server/pgsql/lib/ | grep pgcrypto
pgcrypto.so
ubuntu@ubuntu:~$ ls /www/server/pgsql/share/extension/ | grep pgcrypto
pgcrypto--1.0--1.1.sql
pgcrypto--1.1--1.2.sql
pgcrypto--1.2--1.3.sql
pgcrypto--1.3.sql
pgcrypto.control

很好,整整齐齐

在宝塔面板重启了 PG 之后,连接数据库,运行导入扩展的命令

1
/www/server/pgsql/bin/psql -U postgres
1
CREATE EXTENSION pgcrypto;

但出现了一个奇怪的错误

1
2
postgres=# CREATE EXTENSION pgcrypto;
ERROR: could not load library "/www/server/pgsql/lib/pgcrypto.so": /www/server/pgsql/lib/pgcrypto.so: undefined symbol: EVP_cast5_cbc

EVP_cast5_cbc 是 OpenSSL libcrypto 库自带的一个函数,为什么会缺失呢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ubuntu@ubuntu:~$ openssl version
OpenSSL 3.0.13 30 Jan 2024 (Library: OpenSSL 3.0.13 30 Jan 2024)
ubuntu@ubuntu:~$ ldd /www/server/pgsql/lib/pgcrypto.so
linux-vdso.so.1 (0x00007ffea096a000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x0000754a31f6c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0000754a31c00000)
/lib64/ld-linux-x86-64.so.2 (0x0000754a31fc4000)
ubuntu@ubuntu:~$ ldd /www/server/pgsql/bin/postgres
linux-vdso.so.1 (0x00007ffc553a2000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007b8cf4de1000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007b8cf4317000)
libicui18n.so.74 => /lib/x86_64-linux-gnu/libicui18n.so.74 (0x00007b8cf3e00000)
libicuuc.so.74 => /lib/x86_64-linux-gnu/libicuuc.so.74 (0x00007b8cf3a00000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007b8cf3600000)
/lib64/ld-linux-x86-64.so.2 (0x00007b8cf4e08000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007b8cf3200000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007b8cf4db1000)
libicudata.so.74 => /lib/x86_64-linux-gnu/libicudata.so.74 (0x00007b8cf1400000)
ubuntu@ubuntu:~$ openssl list -cipher-algorithms | grep CAST5
cast => CAST5-CBC
cast-cbc => CAST5-CBC
CAST5-CBC
CAST5-CFB
CAST5-ECB
CAST5-OFB

通过 ldd 查看 pgcrypto.so 使用的共享库并和 postgres 进行比较,pgcrypto.so 并没有链接 libcrypto.so,只链接了 libzlibc,并且 OpenSSL 的 CAST5 是正常的。猜测是宝塔的 pg_config 有问题,或是编译时没有启用 OpenSSL 支持。

于是重新配置,重新编译,尝试通过强制开启 OpenSSL 支持来解决

1
2
3
4
5
6
cd postgresql-16.1
make distclean
./configure --with-pgconfig=/www/server/pgsql/bin/pg_config --with-openssl
cd contrib/pgcrypto
make clean
make

看看有没有 libcrypto.so

1
2
ubuntu@ubuntu:~$ ldd ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.so | grep crypto
libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x0000782303a00000)

那应该没什么问题了,复制过去吧~

1
2
3
sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.so /www/server/pgsql/lib/
sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.control /www/server/pgsql/share/extension/
sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto--*.sql /www/server/pgsql/share/extension/

重启 PG 之后再导入扩展

1
2
3
4
5
6
7
ubuntu@ubuntu:~$ /www/server/pgsql/bin/psql -U postgres
psql (16.1)
Type "help" for help.

postgres=# CREATE EXTENSION pgcrypto;
pgcrypto
postgres=#

大功告成~ 可以愉快的玩耍了 🎉

安装流程

以 PG 16.1 为例

编译

  • 拉取源码并解压

    1
    2
    wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.gz
    tar -zxvf postgresql-16.1.tar.gz
  • 进入解压后的目录,使用宝塔的 pg_config 配置编译环境,并强制使用 OpenSSL

    1
    2
    cd postgresql-16.1
    ./configure --with-pgconfig=/www/server/pgsql/bin/pg_config --with-openssl
  • 进入 pgcrypto 目录,开始编译

    1
    2
    cd contrib/pgcrypto
    make
  • 查看 pgcrypto.so 是否用到了 libcrypto.so

    1
    ldd pgcrypto.so | grep crypto

    应该有类似如下输出:

    1
    2
    ubuntu@ubuntu:~$ ldd ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.so | grep crypto
    libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x0000782303a00000)

安装

  • 复制相关文件至 PG 目录

    1
    2
    3
    sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.so /www/server/pgsql/lib/
    sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto.control /www/server/pgsql/share/extension/
    sudo cp ~/postgresql-16.1/contrib/pgcrypto/pgcrypto--*.sql /www/server/pgsql/share/extension/
  • 在宝塔面板重启 PG 服务

    重启 PG

  • 连接数据库,导入扩展

    1
    /www/server/pgsql/bin/psql -U postgres
    1
    CREATE EXTENSION pgcrypto;
  • 删除临时编译目录(可选)

    1
    rm -rf ~/postgresql-16.1

后记

后记应该是没人看的吧(大概

bash_history

喜欢我 95 行的 bash_history

要是宝塔官方要是能在预构建的 PG 里加入官方的这些扩展就好了

赞助喵
非常感谢您的喜欢!
赞助喵
分享这一刻
让朋友们也来瞅瞅!