PostgreSQL identity vs sequence
Using a sequence for auto-generated unique values is the "old" way and has some drawbacks:
- The sequence is owned by the user who creates it, so other users with access to the database may not be able to use it without extra permissions.
- The link between the table and the sequence must be managed manually. For example, if you drop the table, the sequence may still exist.
- Sequences are not automatically replicated in logical replication setups.
Using an identity column is the modern recommended approach:
- Values are generated automatically from an implicit sequence managed by PostgreSQL.
- Safer: the sequence’s lifecycle is tied directly to the column.
- Can be replicated: physical replication works automatically, and logical replication works if the table is included in the publication (PostgreSQL 15+).
- SQL standard compliant — similar to the standard GENERATED {ALWAYS | BY DEFAULT} AS IDENTITY syntax.
Tip
Use identity columns for new designs. They are cleaner, safer, and compatible with modern replication setups. Sequences should only be used if you need to share them across multiple tables or need manual control over their values and use the same user for all operations and no replication is needed.