SQLCODE Variable - Oracle to PostgreSQL Migration

In Oracle SQLCODE variable contains the error code for the last exception. In PostgreSQL you can use SQLSTATE variable. Note that error code for the same errors can be different.

Oracle:

  CREATE TABLE t1 (c1 INT NOT NULL);
 
  BEGIN
     -- Try to insert NULL into non-nullable column
     INSERT INTO t1 VALUES (NULL); 
  EXCEPTION WHEN OTHERS THEN
   DBMS_OUTPUT.PUT_LINE('Error code is ' || SQLCODE);
  END;
  /
  # Error code is -1400
  # PL/SQL procedure successfully completed.

PostgreSQL:

  CREATE TABLE t1 (c1 INT NOT NULL);
 
  DO $$
  BEGIN
     -- Try to insert NULL into non-nullable column
     INSERT INTO t1 VALUES (NULL); 
  EXCEPTION WHEN OTHERS THEN
    RAISE NOTICE '%','Error code is ' || SQLSTATE;
  END; $$;
  # NOTICE:  Error code is 23502

For more information, see Oracle to PostgreSQL Migration.