python pandas 读取postgresql中的数据

    import psycopg2
    import pandas as pd
    
    # postgres config
    postgres_host = ""               # 数据库地址
    postgres_port = "5432"       # 数据库端口
    postgres_user = ""              # 数据库用户名
    postgres_password = ""      # 数据库密码
    postgres_datebase = ""      # 数据库名字
    postgres_table1 = ""           #数据库中的表的名字

    # connection string
    conn_string = "host=" + postgres_host + " port=" + postgres_port + " dbname=" + postgres_datebase + \
                  " user=" + postgres_user + " password=" + postgres_password
    conn = psycopg2.connect(conn_string)

    sql_command1 = "select * from" .format(postgres_table1)

    try:
        data1 = pd.read_sql(sql_command1, conn)
    except:
        print("load data from postgres failure !")
        exit()

    if data1.shape[0] == 0:
        print("there is no data in !")
发布了29 篇原创文章 · 获赞 16 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/u011412768/article/details/100430080