Adding a new column to a database should be simple. The wrong approach turns it into downtime, failed migrations, or unclear data models. The right approach makes it fast, safe, and repeatable in production.
When you add a new column in SQL—whether MySQL, PostgreSQL, or SQLite—you need to define its name, data type, nullability, and default values. Example:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP DEFAULT NOW();
For large datasets, a blocking ALTER TABLE can lock reads and writes. Use online schema changes when possible:
- In MySQL, tools like
pt-online-schema-changeorgh-ost - In PostgreSQL, adding nullable columns or columns with immutable default values is low-impact
- Avoid adding a column with a non-null default in PostgreSQL without a computed default—it rewrites the full table
Migrations should be part of version control. They should be applied idempotently across all environments. Never run ALTER TABLE manually in production unless there is no alternative.