【发布时间】:2020-11-02 04:30:27
【问题描述】:
抱歉,我是新手,提前感谢您的任何见解。我正在使用sqlalchemy 为Amazon RDS 创建Postgresql 表。当我尝试使用Quicksight 或SQL Workbench 时,表格没有显示。我正在使用的代码是(我也回应了引擎,一切似乎都在工作):
from sqlalchemy import create_engine, inspect, text
import psycopg2
from sqlalchemy.types import Integer, Text, String, DateTime
ENDPOINT="db_instance_name.csnul5z77jon.us-east-1.rds.amazonaws.com"
PORT="5432"
USR="xxx"
PSSWD="xxx"
DBNAME="postgres"
#conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'
conn_string = "postgresql://{}:{}@{}:{}/{}".format(USR, PSSWD, ENDPOINT, PORT, DBNAME)
print(conn_string)
# create engine
try:
engine = create_engine(conn_string)
print("connection OK")
except Exception as e:
print("Database connection failed due to {}".format(e))
df_pull_check.to_sql(
'popular_102',
engine,
schema = 'public',
if_exists='replace',
index=False,
chunksize=500,
dtype={
"date": DateTime,
"symbol": Text,
"name": Text,
"start_popularity": Integer,
"popularity_difference": Float
}
)
with engine.connect() as con:
con.execute('ALTER TABLE popular_102 ADD COLUMN record_id SERIAL PRIMARY KEY')
当我使用下面的代码检查表名时,表popular_102 出现:
inspector = inspect(engine)
table_names = inspector.get_table_names()
print(table_names)
我使用下面的代码检查以确保数据存在,它是:
with engine.connect() as con:
df_sql = pd.read_sql('SELECT * FROM popular_102', con)
df_sql = pd.DataFrame(df_sql, con)
我还在上面的第一个代码部分将to_sql 中的retrieve 更改为append,只是为了触发错误,AWS 日志中确实出现了错误:
[12959]:ERROR: column "record_id" of relation "popular_102" already exists
2020-07-13 01:02:31 xxxxx@postgres:[12959]:STATEMENT: ALTER TABLE popular_102 ADD COLUMN record_id SERIAL PRIMARY KEY
但是,该表永远不会到达 Amazon Quicksight 或 SQL Workbench。我可以在SQL Workbench、commit to Amazon RDS 和access it in Quicksight 中创建一个表。
有什么想法我做错了吗?
【问题讨论】:
-
好像你在使用默认数据库
postgres你在RDS启动期间指定数据库了吗? -
做到了...谢谢!
-
很好,作为答案发布。
标签: pandas postgresql amazon-web-services sqlalchemy amazon-rds