We found that primary keys often lag behind the main table name. This script helped us identify and fix the ones with issues.
select
table_name,
constraint_name ,
'ALTER TABLE ' || table_name || ' RENAME CONSTRAINT ' || constraint_name || ' TO ' || left(table_name, 58) || '_pkey;'
from information_schema.table_constraints tc
where constraint_type = 'PRIMARY KEY'
and constraint_name <> left(table_name, 58) || '_pkey';
This finds all the tables where the primary key name is no longer the "default" pattern (<tablename>_pkey
) and creates a rename script for each.
The 58 character limit above in code above is to account for the maximum size of constraint names (63bytes).
Obviously sense check what is returned prior to running it. Hope that is helpful for others.