I inserted manually two new records setting the id as max (id)+1**
This approach is totally wrong and won't work in any database. It's only worked for you so far in MySQL by sheer luck.
If two connections simultaneously run this, they'll get the same ID. It can only work reliably if you lock the table against concurrent reads so that only one connection can be getting an ID at a time.
It's also terribly inefficient.
This is why sequences exist, so that you can reliably get IDs in the presence of concurrent inserters.
Just use:
INSERT INTO my_table(data1, data2) VALUES ('a','b') RETURNING id;
or:
INSERT INTO my_table(id, data1, data2) VALUES (DEFAULT, 'a','b') RETURNING id;
DEFAULT
is a special place-holder that tell the database to get the default for that column from the table definition. The default is nextval('my_table_id_seq')
, so the next sequence value will get inserted.
Since you're asking basic questions about sequences, I recommend you also consider that sequences are not gapless. It's normal for sequences to have "holes", where the table values go 1, 3, 4, 5, 9, 10, ... .