Adding a new column is one of the most common schema changes. The goal is speed without breaking production. Whether you work with Postgres, MySQL, or a modern cloud database, the process is direct but demands precision.
First, define the column name, type, and default value. Keep names short and explicit—avoid reserved words. Consider nullability. Setting NOT NULL without a default will fail if existing rows lack a value.
In PostgreSQL:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP WITH TIME ZONE DEFAULT NOW();
In MySQL:
ALTER TABLE users ADD COLUMN last_login DATETIME DEFAULT CURRENT_TIMESTAMP;
These commands touch the schema instantly for small tables, but on large datasets, lock times can spike. Test on staging. Use online schema change tools if your database supports them. For PostgreSQL, ALTER TABLE ... ADD COLUMN is fast because it stores only metadata until a non-null default is filled. In contrast, some MySQL versions rewrite the table, so plan the deployment window.