title: Creating primary key on existing table sql
body: <p>I have a table of the following form:</p>
  
  <pre><code>   create table tab1(i1 int, i2 character varying);
  </code></pre>
  
  <p>I want to make i1 as the primary key. However my problem is i1 column contains duplicate rows.
  In order to achieve this I created a table of the following form:</p>
  
  <pre><code>   create table tab2(i1 int, i2 character varying);
  insert into tab2 (i1, i2)
  (select distinct * from tab1);
   alter table tab2 add primary key(i1);
  </code></pre>
  
  <p>After doing so I get the following error:</p>
  
  <pre><code>NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "tab2_pkey" for table "tab2"
  ERROR:  could not create unique index "tab2_pkey"
  DETAIL:  Key (i1)=(1958) is duplicated.
  </code></pre>
  
  <p>Can someone please help me create primary key on tab2?</p>