Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate SQL table with correct column types from CSV
    primarykey
    data
    text
    <p>I've looked a a number of questions on this site and cannot find an answer to the question: How to create <strong>multiple NEW tables</strong> in a database (in my case I am using PostgreSQL) <strong>from multiple CSV source files</strong>, where the new database table columns accurately reflect the data within the CSV columns?</p> <p>I can write the CREATE TABLE syntax just fine, and I can read the rows/values of a CSV file(s), but does a method already exist to inspect the CSV file(s) and accurately determine the column type? Before I build my own, I wanted to check if this already existed.</p> <p>If it doesn't exist already, my idea would be to use Python, CSV module, and psycopg2 module to build a python script that would:</p> <ol> <li>Read the CSV file(s).</li> <li>Based upon a subset of records (10-100 rows?), iteratively inspect each column of each row to automatically determine the right column type of the data in the CSV. Therefore, if row 1, column A had a value of 12345 (int), but row 2 of column A had a value of ABC (varchar), the system would automatically determine it should be a format varchar(5) based upon the combination of the data it found in the first two passes. This process could go on as many times as the user felt necessary to determine the likely type and size of the column.</li> <li>Build the CREATE TABLE query as defined by the column inspection of the CSV.</li> <li>Execute the create table query.</li> <li>Load the data into the new table.</li> </ol> <p>Does a tool like this already exist within either SQL, PostgreSQL, Python, or is there another application I should be be using to accomplish this (similar to pgAdmin3)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload