How to add a primary key to a postgres table in pgadmin

In working with pgadmin, I miss how easy it was to set a column as a primary key in MySQL Workbench. You simply checked off the box under PK (for primary key) (and any other attributes you wanted to have for the column).

Simpler Interface of MySQL Workbench for setting a column's attributes

In pgadmin, I thought that there would be something similar in the Add Column... popup but there isn't any UI (buttons, checkbox) to make it a primary key.

The definitions tab in the Add Column popup does not have any way to set the column as a Primary Key

How do you set a column as a primary key in pgadmin

  1. Create your table and columns
  2. In the Object Browser panel, navigate to the table and select it.
  3. Right-click on the table you want to set the primary key for.
  4. Select New Object from the context menu that pops up.
  5. Select New Primary Key... Set a new primary key by right-clicking on the table, then select New Object, then New Primary Key
  6. In the New Primary Key... Modal Popup that shows up, select the Columns Tab and then select the field/column you want to make as Primary. Select the Column you want to make primary in the Columns Tab
  7. Click on the button labelled Add
  8. Click on the button labelled Ok

What this does is create a Primary Key Index with the column you selected.

Removing a Primary Key in pgadmin

This will be the same as removing any index from a table.

  1. In the Object Browser panel, navigate to the table and select it.
  2. Expand the table you want to modify by clicking on the [+]
  3. Expand on the row labelled Constraints
  4. Right click on the index you want to delete
  5. Select Delete/Drop... Navigating to the Primary Key Constraints in the Object Browser Panel, right click on the primary key to delete.
  6. Confirm that you want to delete the primary key by clicking on the button labelled 'Ok' Confirmation dialog for removing primary key in pgadmin

Daniel Tse

Read more posts by this author.

Subscribe to Daniel Tse, Engineer, Consultant

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!