If I already created some tables with columns, like this:

create table "Sender"(
    "id" varchar not null unique

create table "Message"(
    "sender" varchar not null

-- Oops! The "sender" column should reference the "id" in "Sender" table!

But then I realise one of the columns should actually have a foreign key constraint (reference). Is there a way to add this foreign key constraint without having to "manually" enter a new name for it?

Something along the lines of:

alter table "Message" alter column "sender" set references "Sender"("id");

1 Answer 1


Solved. Posting it here because the postgres docs doesn't actually have this example.

alter table "Message" add foreign key ("sender") references "Sender"("id");

This will add a foreign key constraint named "Message_sender_fkey". Enjoy 🤓


I find myself having to use this when I forgot to add cascading

alter table "Message"
drop constraint "Message_sender_fkey",
add foreign key ("sender") references "Sender"("id") on delete cascade;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.