---
title: "Remember to use primary keys vs unique constraints in Postgres"
description: "Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication."
url: "https://swe.quest/articles/remember-to-use-primary-keys-vs-unique-constraints-in-postgres-429326ad5b407be8"
canonical: "https://neon.com/blog/remember-to-use-primary-keys-vs-unique-constraints-in-postgres"
source: "Neon"
published: "2024-05-15"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["postgres","schema-migrations","replication"]
---

# Remember to use primary keys vs unique constraints in Postgres

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-05-15, 5 min read, How-to. Original: [https://neon.com/blog/remember-to-use-primary-keys-vs-unique-constraints-in-postgres](https://neon.com/blog/remember-to-use-primary-keys-vs-unique-constraints-in-postgres)

## Summary

Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication. Primary keys combine uniqueness with NOT NULL, create an index, act as the usual foreign-key target, and help the query planner. If you rely on UNIQUE, you need to add NOT NULL and run ALTER TABLE ... REPLICA IDENTITY USING INDEX yourself. The simpler habit is id SERIAL PRIMARY KEY, with UNIQUE kept for naturally unique columns like emails or SKUs.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [Replication](https://swe.quest/tags/replication.md) (Distributed systems)

## Prerequisites

- SQL DDL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- relational database constraints ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))

## Keywords

PostgreSQL, PRIMARY KEY, UNIQUE constraint, REPLICA IDENTITY, logical replication, SERIAL

## Related posts

- [Choosing a Postgres Primary Key](https://swe.quest/articles/choosing-a-postgres-primary-key-0faa005e666f2a79.md): Supabase · 2022-09-08 · Medium · 14 min read · PostgreSQL, Databases, Data structures
  Postgres primary key options progress from integer and serial sequences to standards-compliant IDENTITY columns, then to random IDs and UUIDs, each fixing a weakness of the last.
- [Dynamic Table Partitioning in Postgres](https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e.md): Supabase · 2023-10-03 · Medium · 20 min read · PostgreSQL, Schema migrations, SQL, Scaling
  Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock.
- [Postgres to ClickHouse: Data Modeling Tips](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4.md): ClickHouse · 2024-08-28 · Medium · 9 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, SQL, Replication
  Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run.
- [The silent syntax difference in foreign keys between Postgres and MySQL](https://swe.quest/articles/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql-36df1bac8b9567ad.md): Neon · 2024-06-05 · Medium · 6 min read · MySQL, PostgreSQL, SQL, Schema migrations
  MySQL parses but silently ignores inline column-level \`REFERENCES\` clauses, so a foreign key written Postgres-style (\`customer\_id INT REFERENCES customers(customer\_id) ON DELETE CASCADE\`) creates no constraint, no index and no cascade behavior in MySQL.
- [PostgreSQL 15: Three features you can try with Neon](https://swe.quest/articles/postgresql-15-three-features-you-can-try-with-neon-d9568102bec3b3d5.md): Neon · 2022-10-26 · Medium · 3 min read · PostgreSQL, SQL
  Neon now supports PostgreSQL 15, and three of its features can be tried from Neon's SQL Editor with runnable examples.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
