安装psycopg2
1.pip install psycopg
2.下载whl或者.tar.gz文件,安装。下载地址:https://pypi.org/project/psycopg2/#files
whl包:
1.cmd 进 whl包目录
2.pip install **.whl
tar.gz包:
1.解压
2.cmd 进解压目录
3.python setup.py install
连接并进行操作
import psycopg2
conn = psycopg2.connect(host="127.0.0.1", port="5432", user="postgres", password="pass123", database="testdb")
cursor = conn.cursor()
# postgresql语法可参考https://www.runoob.com/postgresql/postgresql-tutorial.html
sql = ''
# write
cursor.execute(sql)
conn.commit()
# read
cursor.execute(sql)
# fetchall():一次性获取所有数据
# fechone():获取一条数据
# fetchmany():一次值提取2000条数据(使用服务端的游标)
result = cursor.fetchall()
conn.commit()
conn.close()