简体   繁体   English

将 CSV 导入 postgreSQL 中的表,忽略重复项 - Amazon AWS/RDS

[英]Import CSV to table in postgreSQL ignoring duplicates - Amazon AWS/RDS

I have a PostgreSQL hosted on AWS (RDS).我有一个 PostgreSQL 托管在 AWS (RDS) 上。 I've created a couple tables and imported some.csv files to this tables using the "Import/Export" tool on PgAdmin4.我创建了几个表并使用 PgAdmin4 上的“导入/导出”工具将一些 .csv 文件导入到该表中。

Monthly I´ll need to update the data on my tables, and I'll do that by uploading.csv files.每个月我都需要更新表格中的数据,我将通过上传 .csv 文件来完成。

The issue that I'm facing right now is: I am trying to insert new data on a table from a.csv file, but I need to ignore the duplicate values .我现在面临的问题是:我正在尝试从 a.csv 文件中的表中插入新数据,但我需要忽略重复值

I have found a way to do that here (code below ) but the copy command does not work on PgAdmin.我在这里找到了一种方法(下面的代码),但是copy命令在 PgAdmin 上不起作用。 Copy only works if I use the import/export tool.复制仅在我使用导入/导出工具时有效。

CREATE TEMP TABLE tmp_table 
ON COMMIT DROP
AS
SELECT * 
FROM indice-id-cnpj
WITH NO DATA;

COPY tmp_table FROM 'C:/Users/Win10/Desktop/Dados/ID-CNPJ.csv';

INSERT INTO indice-id-cnpj
SELECT *
FROM tmp_table
ON CONFLICT DO NOTHING

This is my first experience with PostgreSQL (apart from a subject in uni).这是我对 PostgreSQL 的第一次体验(除了 uni 的一个主题)。 I can deal with the issue by using excel and doing a little manual work, but I'm looking for a " long term " solution, on how to keep updating the tables using the.csv files, always ignoring the duplicates.我可以通过使用 excel 并做一些手工工作来处理这个问题,但我正在寻找一个“长期”解决方案,关于如何使用 .csv 文件继续更新表格,始终忽略重复项。

Thanks in advance.提前致谢。

So, I´ve found a solution.所以,我找到了解决方案。

As Adrian mentioned, I had to use psql.正如 Adrian 提到的,我不得不使用 psql。

CREATE TEMP TABLE tmp_table AS SELECT * FROM table-name WITH NO DATA;
\copy tmp_table FROM 'C:/Users/Win10/folder/filename.csv' DELIMITER ',' CSV ENCODING 'UTF8' ;


INSERT INTO "table-name" SELECT * FROM tmp_table ON CONFLICT DO NOTHING;
DROP TABLE tmp_table;

Since I´m using psql it´s necessary to use the command \copy instead of COPY .由于我使用的是 psql,因此有必要使用命令\copy而不是COPY Also, every command must finish with a ";"此外,每个命令都必须以“;”结尾and it´s necessary to drop the tmp_table at the end.并且有必要在最后删除tmp_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Amazon ORACLE AWS RDS 的 TEMP TABLESPACE 表 DBA_TEMP_FILES 中将 AUTOEXTENSIBLE 列更改为 YES? - How to change the AUTOEXTENSIBLE column to YES in table DBA_TEMP_FILES for TEMP TABLESPACE in Amazon ORACLE AWS RDS? 在 amazon aws RDS 中删除组参数时出现问题 - Problem deleting group parameters in amazon aws RDS pgsync 无法连接到 RDS 上的 Amazon PostgreSQL 数据库 - pgsync cannot connect to Amazon PostgreSQL database on RDS 将 csv 文件从 s3 导入 rds PostgreSQL 数据库 > 错误:模式“aws_commons 不存在 - Importing a csv file from s3 into rds PostgreSQL Database > Error: schema "aws_commons does not exist 在 Amazon RDS 使用 PostgreSQL 进行数据屏蔽,如何? - Data masking using PostgreSQL at Amazon RDS, how to? AWS Lambda 与 RDS Postgresql - 未知问题 - AWS Lambda with RDS Postgresql - Unknown Issue 如何解决 Amazon RDS Postgresql 实例的 DiskFull 错误? - How to resolve Amazon RDS Postgresql instance's DiskFull error? 如何使用 Elasticbeanstalk 在 Amazon RDS 中删除表并重新创建? - How to drop table and recreate in amazon RDS with Elasticbeanstalk? aws_s3.query_export_to_s3 PostgreSQL RDS 扩展使用 header 将所有多部分 CSV 文件导出到 S3 - aws_s3.query_export_to_s3 PostgreSQL RDS extension exporting all multi-part CSV files to S3 with a header 导入 S3 CSV 到 RDS Postgres - Import S3 CSV to RDS Postgres
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM