mac 安装及使用psycopg2小结

学习了下python调用PostgreSQL数据库,PostgreSQL介绍网址如下
https://www.postgresql.org/docs/9.4/static/app-psql.html
psycopg2库下载地址如下:
https://pypi.python.org/pypi/psycopg2
下载解压后terminal cd到解压目录下输入

sudo python setup.py install

后会报错 Error: pg_config executable not found

参考http://blog.csdn.net/muzilanlan/article/details/50421693解决方法

  1. 安装postgresql, 输入
brew install postgresql 
  1. mac系统下默认下载路径是 /usr/local/Cellar/postgresql/9.X.X/bin
    其中9.X.X根据下载的版本不一样 可以利用下图中Finder中的前往文件夹功能找到postgresql文件目录,确认下本机中的上述目录


    1.png
  2. 修改.bash_profile
    .bash_profile 如果存在 则目录可以在terminal中输入cd~到主目录 输入pwd查询到具体地址利用上述前往文件夹的方法找到,利用vim或者sublime打开
    添加

PATH="/usr/local/Cellar/postgresql/9.X.X/bin:${PATH}"
export PATH

保存,若没有该文件,可以建立一个加入上述代码

  1. 回到安装目录输入
sudo python setup.py install

可以成功安装

psycopg2使用语法比较简单,需要注意连接数据库

DB = psycopg2.connect("dbname=forum")

参数需要输入dbname =

sql的关键字需要大写,例如

c = DB.cursor()
c.execute("SELECT a,b FROM xxx ORDER BY a DESC;")

cursor.execute("select name, weight from players,(select avg(weight) as av from players) as subq where weight < av;")

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容