psycopg2关于undefined symbol: lo_truncate64解决方法

时间:2023-03-08 21:01:35

今天,在centos6.5下安装psycopg2,利用Python连接PostgreSQL数据库的时候,出现了一个undefined symbol: lo_truncate6的错误:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: /mydev/pyweb/cancerdb/lib/python2.7/site-packages/psycopg2/_psycopg.so: undefined symbol: lo_truncate64

究其原因,正如 http://initd.org/psycopg/docs/faq.html 所描述:

“This means that Psycopg was compiled with lo_truncate() support (i.e. the libpq used at compile time was version >= 8.3) but at runtime an older libpq dynamic library is found. Fast-forward several years, if the message reports undefined symbol: lo_truncate64 it means that Psycopg was built with large objects 64 bits API support (i.e. the libpq used at compile time was at least 9.3) but at runtime an older libpq dynamic library is found.”

(psycopg在利用lo_truncate()编译的时候需要libpq至少要9.3以上版本,但是在编译的时候却发现了一个版本较低的libpq。)

在网上看到有不少人通过修改psycopg2安装包下的 setup.cfg ,添加PostgreSQL的 pg_config,再重新手动安装psycopg2来解决:

shenweiyan@localhost :: ~
=> vi setup.cfg
.......
# full path.
pg_config=/usr/local/psql-/bin/pg_config

# Set to  to use Python datetime objects for default date/time representation.
use_pydatetime=
.......

尝试才发现该方法不能够从根本上解决该问题,undefined symbol: lo_truncate6的报错依然存在。

经过一番摸索,结合 psycopg2 官方文档,后来才发现需要把PostgreSQL的 libpq.so.5 跟系统的 libpq.so.5 进行替换,再重新安装psycopg2,问题即可解决:

1、find what is the libpq dynamic library used at runtime(查找运行过程中所用的libpq动态库):

$ ldd /path/to/packages/psycopg2/_psycopg.so | grep libpq   

2、You can avoid the problem by using the same version of the pg_config at install time and the libpq at runtime(替换运行过程中的libpq动态库):

root@localhost :: /usr/local/download/psycopg2-
=> cd /usr/lib64
root@localhost :: /usr/lib64
=>
root@localhost :: /usr/lib64
=> /lib/libpq.so. ./

3、重新安装psycopg2,解决问题。

shenweiyan@localhost :: /mydev/pyweb/download/psycopg2-
=> python
Python  (default, Jan  , ::)
[GCC   (Red Hat -)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>>

ok,问题解决!!!