How to make postgres tell which column is causing an error

Viewed 94

While inserting data into a table which has many columns:

INSERT INTO MyTable ("name", ..100+ columns)
 VALUES ('Michel', ... 100+ values)

I made an error creating a specific value so PostgreSQL tells us:

ERROR:  value too long for type character varying(2)

I would like to avoid going through the whole table schema to guess which column is failing.

Is there a way to configure PostgreSQL so it tells us which column is causing the error?

1 Answers

One quick way might be to query the information schema table for your database and look for character columns having a maximum width of 2 (to which your error is alluding):

SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyTable' AND
      character_maximum_length = 2;
Related