Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Auto Loader

Automated ingestion scripts to dynamically load JSONL, Excel, and CSV files into PostgreSQL. They infer schema, create tables, and bulk-load data.

Scripts

1. auto_load_jsonl.py

Dynamically creates a raw table (_raw with the full JSONB data) and a flat table with individually parsed columns. Schema is inferred from the whole file by default (use --sample-size N to scan only the first N objects). Table/index names derived from the filename and JSON keys are sanitized and quoted, and keys colliding with reserved columns (id, raw_id) are renamed. Files with a UTF-8 BOM are handled, and a line that is not valid JSON fails fast with its line number instead of aborting the COPY with an opaque Postgres error. Usage:

uv run python auto_load_jsonl.py <path/to/file.jsonl> [schema_name]
uv run python auto_load_jsonl.py data.jsonl public --sample-size 5000
uv run python auto_load_jsonl.py data.jsonl public --dry-run

2. auto_load_excel.py

Parses .xlsx files, infers PostgreSQL data types (including nested JSON as JSONB), renames column headers based on cell fill colors (e.g. col_yellow), and maps missing cells. Usage:

uv run python auto_load_excel.py <path/to/file.xlsx> [schema_name]
uv run python auto_load_excel.py <path/to/file.xlsx> [schema_name] --dry-run

3. auto_load_csv.py

Parses .csv files with Python's CSV parser so quoted commas, quoted newlines, duplicate headers, blank headers, BOMs, and inconsistent row widths are handled explicitly. It infers conservative PostgreSQL types from the full file by default and bulk-loads rows with COPY.

CSV-specific safeguards:

  • Uses csv.reader instead of pandas to avoid silent coercion of IDs, leading zeros, dates, and large numeric strings.
  • Detects encoding (utf-8-sig, utf-8, cp1252, latin-1) and delimiter (,, ;, tab, |), with overrides available. Delimiter detection validates each candidate by strictly parsing a sample instead of counting character frequencies, so a quoted JSON column full of commas cannot trick it.
  • Fails fast if any row has too few or too many fields, instead of shifting values into the wrong columns.
  • Sanitizes duplicate or blank column names deterministically.
  • Keeps source id columns intact by using loader_id as the loader primary key.
  • Treats empty CSV fields as NULL.
  • Uses NUMERIC rather than floating point for decimal values.
  • Preserves numeric-looking values as TEXT when converting them would be risky, such as values with leading zeros.

Usage:

uv run python auto_load_csv.py <path/to/file.csv> [schema_name]

Dry run without touching Postgres:

uv run python auto_load_csv.py <path/to/file.csv> [schema_name] --dry-run

Optional overrides:

uv run python auto_load_csv.py data.csv public --delimiter "|" --encoding utf-8-sig
uv run python auto_load_csv.py data.csv public --sample-size 5000

By default, CSV schema inference scans all data rows to avoid late-file type surprises. Use --sample-size only when speed matters more than full-file inference.

Setup

  1. Environment Manager: Uses uv.

    uv venv
    uv pip install pandas openpyxl psycopg python-dotenv

    The CSV loader itself only needs psycopg and python-dotenv for database loading.

  2. Environment Variables: Create a .env file in the auto_loader directory with your Postgres credentials (all three scripts load it from there, falling back to the current working directory):

    RAW_DATA_LAYER_HOST=localhost
    RAW_DATA_LAYER_PORT=5433
    RAW_DATA_LAYER_DATABASE=rawdatalayer
    RAW_DATA_LAYER_DBUSER=postgres
    RAW_DATA_LAYER_PASSWORD=your_password

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages