【问题标题】:Read PostgreSQL tables with asyncpg使用 asyncpg 读取 PostgreSQL 表
【发布时间】:2020-03-18 07:26:21
【问题描述】:

我知道如何使用 psycopg2、sqlalchemy、dask 在远程服务器中读取 PostgreSQL 表,但我对读取表的处理时间不满意,并开始研究更快的替代方案,我发现 asyncpg 比除 asyncpg 文档之外的所有方法快 7 倍与上面提到的库相比非常差,那里有很多例子。

我的问题是:如何高效地读取 PostgreSQL 表?

我试过如下:

import asyncio
import asyncpg
import pandas as pd

from sshtunnel import SSHTunnelForwarder #Allow connection with SSH like PuttY connection
from sshtunnel import SSHTunnelForwarder, create_logger #Allow to follow the processes running

SSHTunnelForwarder(('IP_detail', Port_number),
        ssh_private_key=r'path_to_the_ssh_key_in_my_computer',

        ssh_username="username",
        #ssh_password="password", 
        remote_bind_address=('localhost', port_number),
        local_bind_address=('localhost', port_number),
        logger=create_logger(loglevel=1) #Makes processes being ran displayed
                           )

conn = await asyncpg.connect(user='username', password='password',
                                 database='database_name', host='127.0.0.1', port='port')


values = await conn.fetch('''SELECT * FROM table_name''')

values=pd.DataFrame(values)
values

通过上面的代码,我得到了 PostgreSQL 表中每一列的所有行值,但不显示列名,它显示列编号而不是它们的专有名称。 如何纠正这个问题?

【问题讨论】:

    标签: python-3.x postgresql dataframe asyncpg


    【解决方案1】:

    使用dict(values)查看记录和payload的键值对

    【讨论】:

    • 对不起,我不明白你的提示。能详细点吗?
    猜你喜欢
    • 2022-11-24
    • 2022-11-24
    • 2017-11-19
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2023-02-22
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多