Fix “INSERT has more expressions than target columns” in PostgreSQL

If you encounter an error that reads “INSERT has more expressions than target columns” when trying to insert data in Postgres, it’s because you’re trying to insert data into more columns than the table actually contains.

For example, you might be trying to insert four expressions into a table that contains just three columns.

To fix, remove the extra expression/s from your INSERT statement. In other words, make sure you’re inserting the correct number of columns.

Example of Error

Suppose we have a table like this:

+-------+---------+---------+
| petid | petname | pettype |
+-------+---------+---------+
|     1 | Fluffy  | Cat     |
|     2 | Tweet   | Bird    |
+-------+---------+---------+

That table has three columns.

Now, suppose we want to insert another row.

Here’s how to generate the error:

INSERT INTO Pets VALUES ( 3, 'Wag', 'Dog', 'Brown' );

Result:

ERROR:  INSERT has more expressions than target columns
LINE 1: INSERT INTO Pets VALUES ( 3, 'Wag', 'Dog', 'Brown' );
                                                   ^

Here, I tried to insert four expressions into a table that only has three columns.

Solution

The solution is easy. Remove the extra expression:

INSERT INTO Pets VALUES ( 3, 'Wag', 'Dog' );

Here, I removed the last expression (Brown) from the VALUES list. This resulted in the row being inserted without error.

Or, to make sure you don’t inadvertently insert data into the wrong column, you can explicitly state each column:

INSERT INTO Pets ( PetId, PetName, PetType ) 
VALUES ( 1, 'Wag', 'Dog' );

After running one of the above statements, the table now looks like this:

SELECT * FROM Pets;

Result:

+-------+---------+---------+
| petid | petname | pettype |
+-------+---------+---------+
|     1 | Fluffy  | Cat     |
|     2 | Tweet   | Bird    |
|     3 | Wag     | Dog     |
+-------+---------+---------+

We have successfully inserted the row into the table.