今天,在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来解决:
[email protected] 20:13:42 ~ => vi setup.cfg ....... # full path. pg_config=/usr/local/psql-9.4.0/bin/pg_config # Set to 1 to use Python datetime objects for default date/time representation. use_pydatetime=1 .......
尝试才发现该方法不能够从根本上解决该问题,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动态库):
[email protected] 17:26:46 /usr/local/download/psycopg2-2.6.1 => cd /usr/lib64 [email protected] 17:24:09 /usr/lib64 => rm libpq.so.5 [email protected] 17:24:09 /usr/lib64 => ln -s /usr/local/postgresql-9.4.0/lib/libpq.so.5 ./
3、重新安装psycopg2,解决问题。
[email protected] 20:24:34 /mydev/pyweb/download/psycopg2-2.5.4 => python Python 2.7.5 (default, Jan 13 2015, 13:32:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import psycopg2 >>>