pandas - 将 Pandas 数据框上传到 Redshift - 关系 "sqlite_master"不存在

标签 pandas amazon-redshift

我正在尝试将数据框从 Pandas 写入 Redshift 。

这是代码

df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'num_wings': [2, 0, 0, 0],
                   'num_specimen_seen': [10, 2, 1, 8]},
                  index=['falcon', 'dog', 'spider', 'fish'])

from sqlalchemy import create_engine
import sqlalchemy
sql_engine = create_engine('postgresql://username:password@host:port/dbname')
conn = sql_engine.raw_connection()

df.to_sql('tmp_table', conn, index = False, if_exists = 'replace')

但是,我收到以下错误
    ---------------------------------------------------------------------------
    UndefinedTable                            Traceback (most recent call last)
    ~/opt/anaconda3/envs/UserExperience/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
       1594             else:
    -> 1595                 cur.execute(*args)
       1596             return cur

    UndefinedTable: relation "sqlite_master" does not exist
...
...
...

   1593                 cur.execute(*args, **kwargs)
   1594             else:
-> 1595                 cur.execute(*args)
   1596             return cur
   1597         except Exception as exc:

DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': relation "sqlite_master" does not exist

我试过用户 pandas_redshift但是,似乎第一个必须上传到 s3 存储桶,然后再上传到 Redshift 。我想直接上传。同样,Here我看到答案建议先上传到 s3,然后再上传到 redshift

我可以使用相同的连接读取和查询数据库。

最佳答案

尝试使用 sql_engine 而不是 conn。

我刚刚遇到了同样的问题,使用引擎可以解决问题,请尝试以下操作:

import sqlalchemy

engine = sqlalchemy.create_engine('postgres://username:password@url:5439/db_name')
print(bool(engine)) # <- just to keep track of the process

with engine.connect() as conn:
    print(bool(conn)) # <- just to keep track of the process

    df.to_sql(name=table_name, con=engine)
    print("end") # <- just to keep track of the process

enter image description here

关于pandas - 将 Pandas 数据框上传到 Redshift - 关系 "sqlite_master"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58993084/

相关文章:

python - 如何在日期中添加工作日(不包括节假日)

python - Pandas - 自上次以来的行号大于 0 值

python - pandas 版本的 SQL CROSS APPLY

amazon-web-services - 区分 S3 对象创建事件与对象元数据更新

amazon-redshift - 在 dbt 中使用多个数据仓库

SQL:按列分区并在分区内随机排列结果

python - Pandas 数据框的动态合并

python - 有没有办法使用 bool 函数在 Pandas 系列中实现 Action 功能?

sql - 如何在没有 UNION ALL 的情况下跨 redshift 中的多个表进行查询?

amazon-web-services - 使用 PySpark 将数据上传到 Redshift