Automated ingestion scripts to dynamically load JSONL, Excel, and CSV files into PostgreSQL. They infer schema, create tables, and bulk-load data.
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-runParses .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-runParses .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.readerinstead 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
idcolumns intact by usingloader_idas the loader primary key. - Treats empty CSV fields as
NULL. - Uses
NUMERICrather than floating point for decimal values. - Preserves numeric-looking values as
TEXTwhen 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-runOptional 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 5000By 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.
-
Environment Manager: Uses
uv.uv venv uv pip install pandas openpyxl psycopg python-dotenv
The CSV loader itself only needs
psycopgandpython-dotenvfor database loading. -
Environment Variables: Create a
.envfile in theauto_loaderdirectory 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