使用 AWS Pipeline 将本地 postgres 增量复制到 Redshift

Using AWS Pipeline for incremental copy of on premises postgres to Redshift

我们正在探索将 Redshift 用于我们的仓库,我们需要每晚将新数据从现有的本地 postgres 数据库移动到 Redshift。如果您的主数据库在 RDS 中,看起来您可以使用 Pipeline 和模板执行此操作,但如果您的数据库在本地,您可以使用 Pipeline 执行此操作吗?

由于您定期进行批量同步,因此您可能需要考虑将数据推送到 S3,这在本地很容易做到,然后通过 COPY 命令将其加载到 redshift .它快速可靠,作为副作用,您可以在 s3 中免费(便宜)获得备份。

您可以使用 awscli python 工具中的 aws s3 cp 来推送您的 CSV postgres 转储(在将您的访问密钥放入 .aws/config 之后),例如:

aws s3 cp current_dump.csv.gz s3://yourbucket/20170108/dump.csv.gz

然后常规的 postgres psql 实用程序在 redshift 上执行 COPY,如下所示:

PGPASSWORD='YOURPASS' psql -h your.redshift.end.point.com -U youruser -d yourdb -p 5439 -c "COPY yourtable FROM 's3://yourbucket/20170108/dump.csv.gz' CREDENTIALS 'aws_access_key_id=[YOURKEY];aws_secret_access_key=[YOURSECRET]' DELIMITER ',' NULL 'NULL' IGNOREBLANKLINES EMPTYASNULL BLANKSASNULL TIMEFORMAT 'auto' FILLRECORD MAXERROR 1 CSV GZIP;"

您可以将您的 postgres 转储和这些命令放在一个 shell 脚本中,其中包含一些时间值的脚本,并且 运行 它作为一个 cron 作业。

为了获得最佳实践,您希望 upload to a staging table and then merge 到您的主 table 以便在需要时支持更新并防止重复。