首页 > 解决方案 > 将数据从 csv 插入 Postgres 表时出错

问题描述

我在 AWS S3 中上传了一个 csv 文件,我计划使用以下代码更新 Postgres DB 中的表

cursor.execute("""export PGPASSWORD='db_password'; psql -h hostname -U username -d db_name -c "\COPY table_name FROM 's3://filepath.csv' with DELIMITER ','""")

这会引发错误:

psycopg2.errors.SyntaxError: syntax error at or near "export"
LINE 1: export PGPASSWORD='db_password'; psql -h hostname

标签: pythonpostgresqlpsycopg2

解决方案


该命令是 bash 命令,而不是 postgres 命令。


推荐阅读