【Python相关文档】Postgresql数据库接口 - psycopg2,aiopy

Psycopg2

Psycopg 是Python语言的PostgreSQL数据库接口。 它的主要优势在于完全支持Python DB API 2.0,以及安全的多线程支持。它适用于随时创建、销毁大量游标的、和产生大量并发INSERT、UPDATE操作的多线程数据库应用。Psycopg包内含 ZPsycopgDA,一个Zope数据库接口。

源代码安装

参考资料:

http://initd.org/psycopg/docs/install.html

下载二进制源码包:

https://pypi.python.org/pypi/psycopg2/


进入python虚拟环境(可选,如果需要的话)

进入源代码解压后的文件夹

如果pg_config命令不在PATH环境变量中(如果已安装Postgresql,在对应的文件夹中可找到这个命令),修改setup.cfg文件,设置pg_config变量,手动指定pg_config命令的路径(也可以在build参数中指定)

# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
pg_config=/Library/PostgreSQL/9.4/bin/pg_config


运行build命令:

$python setup.py build


运行install命令

$sudo python setup.py install


安装成功后,在对应python环境中的lib/site-packages文件中就会找到安装好的psycopg2文件夹


aiopg

aiopg是一个用于从asyncio (PEP-3156/tulip) 框架访问 PostgreSQL 数据库的Python 开发库。它是Psycopg数据库驱动程序异步功能的封装。

参考资料:

http://aiopg.readthedocs.org/en/stable/

安装

aiopg依赖于psycopg2,需要首先安装psycopg2

可以直接在对应的python环境中使用pip命令安装:

pip3 install aiopg


使用/遇见的问题

Macos中使用时,可能会在运行时出现如下错误信息:

ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib
  Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
  Reason: image not found

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so Library libssl.1.0.0.dylib Library libcrypto.1.0.0.dylib

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _lo_lseek64 Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so Expected in: /usr/lib/libpq.5.dylib in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so

这是由于在 /usr/lib文件夹中找不到对应的mac os动态链接库(或者版本过旧)。

如果已经安装了 postgres, 则在对应的安装文件夹中可以找到这个文件,可以通过如下的方式创建link来解决:


localhost:psycopg2-2.6.1 wongrobin$ sudo ln -s /Library/PostgreSQL/9.4/lib/libssl.1.0.0.dylib /usr/lib
localhost:psycopg2-2.6.1 wongrobin$ sudo ln -s /Library/PostgreSQL/9.4/lib/libcrypto.1.0.0.dylib /usr/lib
localhost:psycopg2-2.6.1 wongrobin$ sudo mv /usr/lib/libpq.5.dylib /usr/lib/libpq.5.dylib.old 
localhost:psycopg2-2.6.1 wongrobin$ sudo ln -s /Library/PostgreSQL/9.4/lib/libpq.5.dylib /usr/lib

参考:

http://stackoverflow.com/questions/16407995/psycopg2-image-not-found

http://stackoverflow.com/questions/28515972/problems-using-psycopg2-on-mac-os-yosemite


示例

__author__ = 'wongrobin'

import asyncio
import logging
from aiopg.pool import create_pool

logging.basicConfig(level=logging.INFO)

dsn = 'dbname=gof user=gof password=gof host=localhost port=5432'

@asyncio.coroutine
def test():
    pool = yield from create_pool(dsn)

    with (yield from pool) as conn:
        cur = yield from conn.cursor()
        yield from cur.execute('SELECT 1')
        ret = yield from cur.fetchone()
        assert ret == (1, ), ret

        logging.info(ret)

loop = asyncio.get_event_loop()
loop.run_until_complete(test())
loop.close()



你可能感兴趣的:(python)