美文网首页
Python访问PostgreSQL

Python访问PostgreSQL

作者: 十毛tenmao | 来源:发表于2021-01-20 22:06 被阅读0次

Python访问MySQL一般都使用pymysql,访问PostgreSQL也有很多驱动,其中psycopg2使用最广泛

安装

pip install psycopg2

访问示例

# coding=utf-8

import psycopg2

# 创建连接
conn = psycopg2.connect(host='100.76.84.71', user='test', password='test', dbname='test', port=8081)

# 获取游标
cursor = conn.cursor()

# 执行语句
cursor.execute("SELECT VERSION()")

# 获取结果:支持fetchone, fetchall和fetchmany
res = cursor.fetchone()
print(res)

# 关闭连接
conn.close()

常见问题

  • SSLError WRONG_VERSION_NUMBER
    使用pip20.3+版本后,代理会失效,一般推荐做法是改进https_proxy,但是本人一直没有成功。最后选择了降级:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
py get-pip.py pip==20.2.4

参考

相关文章

网友评论

      本文标题:Python访问PostgreSQL

      本文链接:https://www.haomeiwen.com/subject/hzylzktx.html