Adding a new column to a database table sounds simple, but one wrong step can lock tables, drop data, or tank performance. Whether you are working with PostgreSQL, MySQL, or SQLite, the process must be precise. An ALTER TABLE statement is the core tool. The syntax varies slightly by database, but the concept is constant: define the column name, type, constraints, and default values in one atomic change if possible.
Example for PostgreSQL:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP WITH TIME ZONE DEFAULT NOW();
Before adding a new column in production, check the table size and downtime risk. Large tables can cause migrations to run for minutes or hours. Use CONCURRENTLY options or create the column without defaults, then backfill in small batches. This reduces lock contention and keeps services responsive.
Schema changes should be version-controlled. Generate migration scripts, review them in code review, and test against a realistic staging dataset. Roll forward; avoid rollbacks unless the downtime is acceptable.