Adding a new column in a production database is simple in syntax but dangerous in effect. Schema changes can block queries, lock tables, and trigger unexpected issues if you don’t plan them. Whether you work with PostgreSQL, MySQL, or SQLite, the approach must balance accuracy, speed, and zero downtime.
In PostgreSQL, the base command looks like this:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This runs fast if the column allows nulls and has no default. The database only updates its metadata. Adding a non-null column with a default, however, can rewrite the entire table—potentially locking it for minutes or hours. For large datasets, that’s unacceptable. Instead, create the column as nullable, backfill data in small batches, then set constraints.
In MySQL, the principle is similar. Use:
ALTER TABLE orders ADD COLUMN processed_at DATETIME NULL;
But MySQL’s ALTER operations may still copy the table internally. For massive tables, use an online schema change tool like pt-online-schema-change or native ALGORITHM=INPLACE options.