How to retrieve the last inserted row’s ID using the “INSERT INTO … RETURNING” statement in PostgreSQL?

1 Answers
Answered by suresh

Retrieving Last Inserted Row's ID in PostgreSQL using RETURNING

How to Retrieve the Last Inserted Row's ID in PostgreSQL using RETURNING

To retrieve the ID of the last inserted row in PostgreSQL using the "INSERT INTO ... RETURNING" statement, you can use the following query:

      
        INSERT INTO your_table_name (column1, column2, ...)
        VALUES (value1, value2, ...)
        RETURNING id;
      
    

In this query, replace "your_table_name" with the actual name of your table, and specify the columns and values you want to insert. By adding "RETURNING id", PostgreSQL will return the ID of the last inserted row.

This method is useful when you need to know the ID of the record you just inserted for further processing or referencing.

Answer for Question: How to retrieve the last inserted row’s ID using the “INSERT INTO … RETURNING” statement in PostgreSQL?