The closest thing to MongoDB's ObjectId in PostgreSQL is the uuid
type. Note that ObjectId has only 12 bytes, while UUIDs have 128 bits (16 bytes).
You can convert your existsing IDs by appending (or prepending) f.ex. '00000000'
to them.
alter table some_table
alter id_column
type uuid
using (id_column || '00000000')::uuid;
Although it would be the best if you can do this while migrating the schema + data. If you can't do it during the migration, you need to update you IDs (while they are still varchar
s: this way the referenced columns will propagate the change), drop foreign keys, do the alter type
and then re-apply foreign keys.
You can generate various UUIDs (for default values of the column) with the uuid-ossp
module.
create extension "uuid-ossp";
alter table some_table
alter id_column
set default uuid_generate_v4();