1

Uploaded a csv file to postgresql database and trying to change the columns that contain numbers from default text to numeric datatype.

I understand that you can manually alter each individual column datatype using ALTER and adding "," for multiple columns. However, the csv file contains 50 columns so trying to find out if there's a more DRY approach to changing the datatypes.

I was thinking about looping through certain sections of the csv file that need to be changed from text to numeric. For example, if columns 10 through 25 (out of 50) need to be changed, how would I select the starting point at column 10 and end at column 25?

If there's a way to loop through all columns and change the data type depending on the values in the column, that would be good to know. I was thinking this might be a problem because all values are set to default "text", so determining datatype based on values would be problematic. If this is not the case, would like to know how to approach the problem this way. Thanks!

Glenn G.
  • 419
  • 3
  • 7
  • 18
  • 1
    The CSV doesn't create the table. you must be using a third party tool, so you'll have to tell us how the tables are being created. – Evan Carroll Apr 18 '18 at 22:30
  • Read this: [How to generate a schema from a CSV for a PostgreSQL Copy](https://stackoverflow.com/a/38688898/1995738) – klin Apr 18 '18 at 22:36

0 Answers0