Adding a new column sounds simple, but the wrong approach will lock your table, slow your database, or break production workflows. Whether you’re working with PostgreSQL, MySQL, or SQLite, the way you add a column depends on scale, data type, defaults, and nullability.
In PostgreSQL, the basic syntax is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This creates the column instantly for most metadata-only changes. Adding a column with a non-null default, however, rewrites the entire table. On tables with millions of rows, that can become a major operation. To avoid long locks, first add the column as nullable, then backfill in small batches, and finally enforce constraints.
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
-- Backfill in batches
UPDATE users SET last_login = NOW() WHERE last_login IS NULL;
ALTER TABLE users ALTER COLUMN last_login SET NOT NULL;
MySQL behaves differently. Before 8.0, adding a column almost always copied the table. Since 8.0 with ALGORITHM=INSTANT, you can add certain columns without rebuilding, but only at the end of the table and with nullable defaults. Understanding your database’s internal mechanics is critical to avoid downtime.