【问题标题】:Execution failed on sql : insert dataframe to postgresql serversql 执行失败:将数据帧插入到 postgresql 服务器
【发布时间】:2019-02-11 08:57:43
【问题描述】:

我正在尝试连接 postgresql 服务器并将一些数据帧插入到表中,这是我的代码

import datetime
import gzip
import logging
import os
import sys
import traceback
import numpy as np
import pandas as pd
import psycopg2
from pandas.io.sql import SQLTable, pandasSQL_builder
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from sqlalchemy import create_engine

c = psycopg2.connect(database="dc_integration", user="mastername", 
host="integrationdb.c46zksctipg7.ap-southeast-1.rds.amazonaws.com", 
password="xxx")

d = {'project_key': [1, 2], 'project_name': [3, 4], 'update_date': 
['2016-06-22 19:10:28','2016-06-22 19:10:15']}
df = pd.DataFrame(data=d)
df.to_sql('project', con=c)

但我得到了这个错误

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

但我可以从表中读取数据。我不知道为什么会失败。

【问题讨论】:

  • relation "sqlite_master" 不存在”有什么不清楚的地方?

标签: postgresql pandas pandas-to-sql


【解决方案1】:

我认为您应该将您的 search_path 设置为您的表所在的架构。

任一个:

ALTER USER username SET search_path = schema;

在 PostgreSQL 中。

或者在psycorg2.connect的参数中设置你的search_path:

search_path=schema

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-11
    • 2021-12-20
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多