Postgres how to reference FK to primary composite key? Having a table definition like:
CREATE TABLE foo
(
    id1 Integer NOT NULL,
    id2 Integer NOT NULL    
    PRIMARY KEY (id1, id2)
);

I found it impossible for linking a foreign key from another table to refer to each of the composite primary key above separately.
I get there is no unique constraint matching given keys for referenced table.
So I tried up with new table definition like:
CREATE TABLE foo
(
id1 Integer NOT NULL,
id2 Integer NOT NULL    
UNIQUE (id1, id2),
PRIMARY KEY (id1, id2)
};

it didn't go well, so I ended up with- although not sure it is ok as I already have a primary composite key:
CREATE TABLE foo
(
id1 Integer NOT NULL,
id2 Integer NOT NULL    
UNIQUE (id1),
UNIQUE (id2),
PRIMARY KEY (id1, id2)
};

What am I doing wrong? I want uniqueness in terms of composite primary key not on the seperated fields.
A foreign key constraint can only reference a unique or primary key constraint.
So if your primary key is defined on (id1, id2) and neither id1 nor id2 are unique, there is no way that you can reference the individual columns in a foreign key.
If, on the other hand, id1 and id2 are unique, then one of them should be the primary key, not the combination of both.
It is not clear what you want to achieve, but I'd suspect that the problem goes away when you normalize the tables.