How do I ALTER a PostgreSQL table and make a column unique?

Baishampayan Ghose picture Baishampayan Ghose · Jan 22, 2009 · Viewed 130.7k times · Source

I have a table in PostgreSQL where the schema looks like this:

CREATE TABLE "foo_table" (
    "id" serial NOT NULL PRIMARY KEY,
    "permalink" varchar(200) NOT NULL,
    "text" varchar(512) NOT NULL,
    "timestamp" timestamp with time zone NOT NULL
)

Now I want to make the permalink unique across the table by ALTER-ing the table. Can anybody help me with this?

TIA

Answer

Baishampayan Ghose picture Baishampayan Ghose · Jan 22, 2009

I figured it out from the PostgreSQL docs, the exact syntax is:

ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn);

Thanks Fred.