Adding a new column to a database table is one of the most common schema changes in software development, yet it can wreck production if done without precision. Whether you’re working with PostgreSQL, MySQL, or another relational database, the steps are simple in theory but brutal if you miss a detail.
In SQL, creating a new column is done with ALTER TABLE. For example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP;
This command changes the schema instantly on small tables. On large, high-traffic tables, it can lock writes or cause downtime if your database engine requires a full table rewrite. The risk grows when you add NOT NULL constraints without default values, because existing rows must be updated immediately.
Safe approaches often include:
- Adding the column as nullable.
- Backfilling data in small batches.
- Applying constraints after the backfill is complete.
For PostgreSQL, ALTER TABLE ... ADD COLUMN is fast for nullable columns, but adding defaults can still cause a rewrite unless using version 11+ with constant defaults. For MySQL, using ADD COLUMN can be instant with certain storage engines and settings, but it’s not guaranteed—especially if you're also reordering columns or changing data types.