csv2pg: CSV → PostgreSQL without the shell dance

·2 min read
csv2pg: CSV → PostgreSQL without the shell dance

Most “import this CSV” advice ends with psql invocations, \copy meta-commands, and a schema you hand-write after eyeballing the data in a spreadsheet. I got tired of that ritual, so csv2pg does the whole path in-process through psycopg3: analyze the file, infer the schema, create the table, stream the rows, verify the count, commit. No psql binary involved anywhere.

The interesting part is the type inference. It samples up to 100 random rows per column to guess a candidate PostgreSQL type, then validates that guess against every row in the file. If validation fails, the column widens to the next compatible type — up to five attempts — before falling back to text. You get a tabulated summary plus a generated .sql file before anything touches the database.

Features:

  • Multi-pass type inference with validation against the full file, not just a sample
  • Loads via PostgreSQL’s native COPY ... FROM STDIN protocol — fast on real-sized files
  • Single transaction end to end: any failure rolls back completely, no partial tables left behind
  • --autoimport for scripted runs, --dry-run to validate everything without writing
  • Custom target schema (--schema), auto-created if missing
  • Credentials via a single libpq $CONDB string; passwords redacted from logs automatically
  • Every run produces timestamped .sql and .log artifacts
  • Unit tests run database-free; integration tests activate when $CONDB is set

Source: github.com/rahlquist/csv2pg · MIT licensed.