python中使用postgres

本文不详细介绍如何用python连接并操作postgres数据库,只介绍如何在Python下安装postgres数据库接口,以及在安装过程中遇到的一些问题。

我其中博客介绍了利用easy_install来安装mysql数据库接口。如果没有安装easy_install的童鞋,请参考博文: Ubuntu下安装easy_install 和 mysql-python

和Mysql一样,用Python连接postgres数据库也需要对应的连接库。我采用的是psycopg2 ,如果有童鞋不想用easy_install安装,上面已经给出了psycopg2的官方网址,里面也介绍了其他的安装方法以及详细的使用文档。下面开始介绍利用easy_install安装psycopg2的过程:

注:以下过程是在64位Ubuntu12.04机器上实验的,32位系统应该也适用,系统未安装postgres数据库,默认已经安装了easy_install。

sudo easy_install psycopg2

在没有安装postgres开发环境的机器上很可能出现下面的错误

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.
error: Setup script exited with 1

提示说,pg_config executable没有找到,google一下,发现需要另外安装一个postgres的开发库(我是这么理解的~)

sudo apt-get install libpq-dev

安装完成后,再次安装 psycopg2,postgres for python的开发环境就已经配置好了,在开发中如果想连接postgres数据库,直接import psycopg2即可,关于psycopg2的详细使用方法可访问我上面提供的网址

本文来自博客园,如果查看原文请点击 http://www.cnblogs.com/meibenjin/archive/2013/05/05/3061147.html

原文地址:https://www.cnblogs.com/meibenjin/p/3061147.html