PostgreSQL Alter table add foreign key constraint to a table

Add foreign key constraint to table

How to add foreign key constraint to an existing table. Constraint syntax and example.

Syntax

ALTER TABLE table_name1 
ADD CONSTRAINT fk_name
FOREIGN KEY (column_name) 
REFERENCES table_name2 (unique_column_name);

Example

ALTER TABLE customers 
ADD CONSTRAINT fk_address 
FOREIGN KEY (address_id) 
REFERENCES customer_address (id);