Add Primary Key Column to PostgreSQL Table

The other day I got a question:

How to add a numeric PK column to an already existent table?

A quick look on the net showed that it is indeed possible to add an auto-incrementing PK to an already existing table (if you don’t have already a PK that is!). The code is quite simple too!

The statement:

will add an autoincrementing column named id to my table_without_pk.


PS: Thanks to a couple of colleagues of mine for asking the question and working together to finding the answer :)

A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top