Skip to main content

Lean Aurora PostgreSQL to Redshift loader via copy_expert + S3 COPY

Project description

aurora_to_rs

Lean Aurora PostgreSQL to Redshift loader using psycopg2.copy_expert + S3 + Redshift COPY.

Bypasses pandas entirely -- ~10x faster than DataFrame-based approaches for large tables. IAM role auth only.

Install

pip install aurora_to_rs

Quick Start

from aurora_to_rs import aurora_to_rs

loader = aurora_to_rs(
    region_name='ap-south-1',
    s3_bucket='my-bucket',
    redshift_c=redshift_conn,          # psycopg2 connection (autocommit=True)
    postgres_engine=pg_engine,          # SQLAlchemy engine
    iam_role_arn='arn:aws:iam::123456:role/MY_ROLE'
)

3 Strategies

1. full_load -- Full table refresh (TRUNCATE + COPY)

For master/reference tables where filter_cond = '1=1'.

loader.full_load("SELECT * FROM master.items", "master.items")

2. delete_and_insert -- Delete by filter + COPY

For transaction tables with del_table = 'Y' (incremental by date/timestamp filter).

loader.delete_and_insert(
    "SELECT * FROM tran.shipment_au WHERE create_datetime >= current_date-3",
    "tran.shipment_au",
    "create_datetime >= current_date-3",
    min_timestamp='2026-02-19',          # optional safety overlap
    timestamp_col='create_datetime'
)

3. upsert -- Staging-based incremental upsert

For tables with key-based upsert (everything else). NULL-safe key matching.

loader.upsert(
    "SELECT * FROM tran.orders WHERE dt >= current_date-1",
    "tran.orders",
    ['order_id']
)

How It Works

Aurora (PG COPY TO STDOUT)  -->  BytesIO (strip \x00, parse header)  -->  S3  -->  Redshift COPY FROM
  • Streams CSV via psycopg2.copy_expert -- no pandas DataFrame in the path
  • Strips null bytes (\x00) from Aurora text columns automatically
  • Cleans column names (/, ., - replaced with _) to match Redshift DDL
  • S3 upload with retry (exponential backoff on SSL/connection errors)
  • All methods return row_count for logging

API Reference

Method Args Returns Use Case
full_load(sql, dest) source SQL, dest table row_count Full refresh
delete_and_insert(sql, dest, filter, ...) + min_timestamp, timestamp_col row_count Incremental by date
upsert(sql, dest, keys) + upsert_columns list row_count Key-based upsert

Requirements

  • Python >= 3.8
  • boto3, psycopg2
  • IAM role with S3 read/write and Redshift COPY permissions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aurora_to_rs-0.1.4.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aurora_to_rs-0.1.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file aurora_to_rs-0.1.4.tar.gz.

File metadata

  • Download URL: aurora_to_rs-0.1.4.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for aurora_to_rs-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d1f0c2ebb9b53fb514732ae8c020f80f3af3ceab8f5b78585ebc2a055a8ba030
MD5 3a4f9e61e080c638e7c5867782ce6065
BLAKE2b-256 2cdaa87837fb46fb2d13e8db3cbad4289e94aa8cc5f0668b120afb3589d25212

See more details on using hashes here.

File details

Details for the file aurora_to_rs-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: aurora_to_rs-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for aurora_to_rs-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b93cd4db962bf9e634ba2f032853331fe3cc37a90787cb5e2a4b0d11d001cee2
MD5 266587bcb942dbfeaf2d26be0c977c11
BLAKE2b-256 7ad9608882029d0f09de3bb58462102275eb94916cc4da5a3d41c1d612adabe5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page