1. 问题描述

​ python项目要获取greenplum数据库数据,gp底层是postgresql,需要使用python的第三方工具包psycopg2操作数据库,但是问题是服务器上没有网络,无法在线安装,试了N中方法,最后才用whl安装成功。

2. 解决方案

2.1 官网下载whl文件

网址:
https://pypi.org/project/psycopg2-binary/#files

选择下载:
psycopg2_binary-2.8.3-cp35-cp35m-manylinux1_x86_64.whl (2.9 MB)  Copy SHA256 hash SHA256

2.2 安装

pip install psycopg2_binary-2.8.2-cp37-cp37m-manylinux1_x86_64.whl 

2.3 操作数据库

## 导入psycopg2包
import pandas as pd
import psycopg2

def queryGp(sql):
    ## 连接到一个给定的数据库
    conn = psycopg2.connect(dbname="db-laowang", user="laowang", password="123321",
                         port="5432", host="192.168.0.11", client_encoding="UTF-8")

    df = pd.read_sql(sql, con=conn)
    ## 关闭数据库连接
    conn.close()
    return df

示例代码是连接上数据库,然后执行拼接的sql,返回数据集合。


相关文章:

  • 2021-10-26
  • 2021-09-18
  • 2021-08-07
  • 2022-12-23
  • 2022-01-18
  • 2021-06-13
  • 2021-04-01
猜你喜欢
  • 2021-12-03
  • 2021-12-31
  • 2021-09-01
  • 2021-11-07
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案