Adding a new column in a live database should be simple, but speed and safety demand a strategy. Done wrong, downtime and data loss follow. Done right, production keeps running without a hitch.
A new column changes the shape of your data. In PostgreSQL, you can add it with:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This runs instantly for many column types, but large tables or certain constraints can lock writes. For MySQL, the syntax is:
ALTER TABLE users ADD COLUMN last_login DATETIME;
In older MySQL versions, adding columns could rebuild the table, blocking queries. Modern engines like InnoDB with ALGORITHM=INPLACE reduce the impact:
ALTER TABLE users ADD COLUMN last_login DATETIME, ALGORITHM=INPLACE, LOCK=NONE;
Always set a default value and NULL rules deliberately. Defaults apply to new rows, not existing ones, unless you explicitly update them. For nullable columns: